home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / cc / g++-dist / cplus-init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-25  |  107.4 KB  |  3,519 lines

  1. /* Handle initialization things in C++.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.    Contributed by Michael Tiemann (tiemann@mcc.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* High-level class interface. */
  23.  
  24. #include "config.h"
  25. #include "tree.h"
  26. #include "cplus-tree.h"
  27. #include "flags.h"
  28. #include "assert.h"
  29.  
  30. /* For expand_asm_operands.  */
  31. extern char *input_filename;
  32. extern int lineno;
  33.  
  34. #define NULL 0
  35.  
  36. /* In C++, structures with well-defined constructors are initialized by
  37.    those constructors, unasked.  CURRENT_BASE_INIT_LIST
  38.    holds a list of stmts for a BASE_INIT term in the grammar.
  39.    This list has one element for each base class which must be
  40.    initialized.  The list elements are [basename, init], with
  41.    type basetype.  This allows the possibly anachronistic form
  42.    (assuming d : a, b, c) "d (int a) : c(a+5), b (a-4), a (a+3)"
  43.    where each successive term can be handed down the constructor
  44.    line.  Perhaps this was not intended.  */
  45. tree current_base_init_list, current_member_init_list;
  46.  
  47. void init_init_processing ();
  48. void finish_base_init ();
  49. static void expand_aggr_vbase_init ();
  50. void expand_member_init ();
  51. void expand_aggr_init ();
  52. tree build_virtual_init ();
  53. tree build_vbase_delete ();
  54.  
  55. static void expand_aggr_init_1 ();
  56. static void expand_recursive_init_1 ();
  57. static void expand_recursive_init ();
  58. tree expand_vec_init ();
  59. tree build_vec_delete ();
  60.  
  61. static void add_friend (), add_friends ();
  62.  
  63. int is_aggr_typedef ();
  64. /* Cache _builtin_new and _builtin_delete exprs.  */
  65. static tree BIN, BIVN, BID;
  66. static tree USR_NEW, USR_VNEW;
  67. static tree build_user_new ();
  68.  
  69. #ifdef SOS
  70. tree get_linktable_name (), get_dtable_name (), get_sos_dtable ();
  71. static tree __sosFindCode, __sosLookup, __sosImport;
  72. static tree build_dynamic_new ();
  73. #endif
  74. static tree minus_one;
  75.  
  76. extern struct rtx_def *start_sequence (), *get_insns (), *get_last_insn ();
  77. extern struct rtx_def *const0_rtx;
  78.  
  79. /* Set up local variable for this file.  MUST BE CALLED AFTER
  80.    INIT_DECL_PROCESSING.  */
  81.  
  82. void init_init_processing ()
  83. {
  84.   BIN = default_conversion (TREE_VALUE (lookup_name (get_identifier ("__builtin_new"))));
  85.   BIVN = default_conversion (TREE_VALUE (lookup_name (get_identifier ("__builtin_vec_new"))));
  86.   BID = default_conversion (TREE_VALUE (lookup_name (get_identifier ("__builtin_delete"))));
  87.   USR_NEW = get_identifier ("__user_new");
  88.   USR_VNEW = get_identifier ("__user_vec_new");
  89.   minus_one = build_int_2 (-1, -1);
  90. #ifdef SOS
  91.   if (flag_all_virtual == 2)
  92.     {
  93.       __sosFindCode = default_conversion (lookup_name (get_identifier ("sosFindCode")));
  94.       __sosLookup = default_conversion (lookup_name (get_identifier ("sosLookup")));
  95.       __sosImport = default_conversion (lookup_name (get_identifier ("sosImport")));
  96.     }
  97. #endif
  98. }
  99.  
  100. /* Perform whatever initialization have yet to be done on the
  101.    base class of the class variable.  These actions are in
  102.    the global variable CURRENT_BASE_INIT_LIST.  Such an
  103.    action could be NULL_TREE, meaning that the user has explicitly
  104.    called the base class constructor with no arguments.
  105.  
  106.    If there is a need for a call to a constructor, we
  107.    must surround that call with a pushlevel/poplevel pair,
  108.    since we are technically at the PARM level of scope.
  109.  
  110.    Argument ASSIGNS_THIS_P is nonzero if the current function assigns
  111.    `this' explicitly.  We cannot get this value by checking
  112.    `current_function_assigns_this', since it is set up after this
  113.    function is called.  (although I don't know if its really
  114.    necessary to wait until afterward to do that.)
  115.  
  116.    Note that finish_base_init does *not* initialize virtual
  117.    base classes.  That is done specially, elsewhere.  */
  118.    
  119. void
  120. finish_base_init (t, assigns_this_p)
  121.      tree t;
  122.      int assigns_this_p;
  123. {
  124.   tree member, decl;
  125.   tree init_list, basetype;
  126.   int pass, start;
  127.   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
  128.   tree fields_to_unmark = NULL_TREE;
  129.  
  130.   /* We only care about constructors.  */
  131.   assigns_this_p &= DECL_CONSTRUCTOR_P (current_function_decl);
  132.  
  133.   start = ! TYPE_USES_VIRTUAL_BASECLASSES (t);
  134.   for (pass = start; pass < 2; pass++)
  135.     {
  136.       tree vbase_init_list = NULL_TREE;
  137.       for (init_list = current_base_init_list; init_list;
  138.        init_list = TREE_CHAIN (init_list))
  139.     {
  140.       tree basename = TREE_PURPOSE (init_list);
  141.       tree basetype;
  142.       tree init = TREE_VALUE (init_list);
  143.  
  144.       if (basename == NULL_TREE)
  145.         {
  146.           /* Initializer for single base class.  Must not
  147.          use multiple inheritance or this is ambiguous.  */
  148.           switch (n_baseclasses)
  149.         {
  150.         case 0:
  151.           error ("type `%s' does not have a base class to initialize",
  152.              IDENTIFIER_POINTER (current_class_name));
  153.           return;
  154.         case 1:
  155.           break;
  156.         default:
  157.           error ("unnamed initializer ambiguous for type `%s' which uses multiple inheritance", IDENTIFIER_POINTER (current_class_name));
  158.           return;
  159.         }
  160.           basetype = CLASSTYPE_BASECLASS (t, 1);
  161.         }
  162.       else if (is_aggr_typedef (basename, 1))
  163.         {
  164.           basetype = basetype_or_else (TREE_TYPE (TREE_TYPE (basename)), t);
  165.           if (basetype == NULL_TREE)
  166.         continue;
  167.  
  168.           /* Virtual base classes are special cases.  Their initializers
  169.          are recorded with this constructor, and they are used when
  170.          this constructor is the top-level constructor called.  */
  171.           if (! TREE_VIA_VIRTUAL (basetype))
  172.         {
  173.           /* Otherwise, if it is not an immediate base class, complain.  */
  174.           for (i = n_baseclasses; i > 0; i--)
  175.             if (basetype == CLASSTYPE_BASECLASS (t, i))
  176.               break;
  177.           if (i == 0)
  178.             {
  179.               error ("type `%s' is not an immediate base class of type `%s'",
  180.                  IDENTIFIER_POINTER (basename),
  181.                  IDENTIFIER_POINTER (current_class_name));
  182.               continue;
  183.             }
  184.         }
  185.         }
  186.       else
  187.         continue;
  188.  
  189.       /* The base initialization list goes up to the first
  190.          base class which can actually use it.  */
  191.  
  192.       if (CLASSTYPE_MARKED6 (basetype))
  193.         {
  194.           error ("class `%s' initializer already specified",
  195.              IDENTIFIER_POINTER (basename));
  196.           continue;
  197.         }
  198.  
  199.       if (pass == start)
  200.         {
  201.           char *msgp = (! TYPE_HAS_CONSTRUCTOR (basetype))
  202.         ? "cannot pass initialization up to class `%s'" : 0;
  203.  
  204.           while (! TYPE_HAS_CONSTRUCTOR (basetype)
  205.              && CLASSTYPE_N_BASECLASSES (basetype) == 1)
  206.         basetype = CLASSTYPE_BASECLASS (basetype, 1);
  207.  
  208.           if (basetype)
  209.         {
  210.           if (msgp)
  211.             if (pedantic)
  212.               {
  213.             error_with_aggr_type (basetype, msgp);
  214.             continue;
  215.               }
  216.             else if (! TYPE_HAS_CONSTRUCTOR (basetype))
  217.               {
  218.             if (CLASSTYPE_N_BASECLASSES (basetype) == 0)
  219.               error_with_aggr_type (basetype, msgp);
  220.             else
  221.               sorry ("passing initializations up multiple inheritance lattice");
  222.             continue;
  223.               }
  224.         }
  225.           else
  226.         {
  227.           error ("no constructor found for initialization of `%s'",
  228.              IDENTIFIER_POINTER (basename));
  229.           continue;
  230.         }
  231.  
  232.           if (TREE_VIA_VIRTUAL (basetype))
  233.         {
  234.           CLASSTYPE_MARKED6 (basetype) = 1;
  235.           vbase_init_list = tree_cons (init, basetype, vbase_init_list);
  236.         }
  237.           if (pass == 0)
  238.         continue;
  239.         }
  240.       else if (TREE_VIA_VIRTUAL (basetype))
  241.         continue;
  242.  
  243.       CLASSTYPE_MARKED6 (basetype) = 1;
  244.       member = convert_pointer_to (basetype, current_class_decl);
  245.       expand_aggr_init_1 (t, 0,
  246.                   build_indirect_ref (member, 0), init,
  247.                   ! DECL_OFFSET (TYPE_NAME (basetype)),
  248.                   LOOKUP_PROTECTED_OK|LOOKUP_COMPLAIN);
  249.     }
  250.  
  251.       if (pass == 0)
  252.     {
  253.       tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
  254.       tree vbases;
  255.  
  256.       if (DECL_COMPILER_GENERATED_P (current_function_decl)
  257.           && TREE_CHAIN (first_arg) != NULL_TREE)
  258.         {
  259.           /* If there are virtual baseclasses without initialization
  260.          specified, and this is a default X(X&) construcotr,
  261.          build the initialization list so that each virtual baseclass
  262.          of the new object is initialized from the virtual baseclass
  263.          of the incoming arg.  */
  264.           tree init_arg = build_unary_op (ADDR_EXPR, TREE_CHAIN (first_arg), 0);
  265.           for (vbases = CLASSTYPE_VBASECLASSES (t);
  266.            vbases; vbases = TREE_CHAIN (vbases))
  267.         {
  268.           basetype = TREE_TYPE (vbases);
  269.           if (CLASSTYPE_MARKED6 (basetype) == 0)
  270.             {
  271.               member = convert_pointer_to (basetype, init_arg);
  272.               if (member == init_arg)
  273.             member = TREE_CHAIN (first_arg);
  274.               else
  275.             TREE_TYPE (member) = build_reference_type (basetype);
  276.               vbase_init_list = tree_cons (convert_from_reference (member),
  277.                            basetype, vbase_init_list);
  278.               CLASSTYPE_MARKED6 (basetype) = 1;
  279.             }
  280.         }
  281.         }
  282.       expand_start_cond (first_arg, 0);
  283.       expand_aggr_vbase_init (t, C_C_D, current_class_decl,
  284.                   vbase_init_list);
  285.       expand_expr_stmt (build_vbase_vtables_init (t, t, C_C_D,
  286.                               current_class_decl));
  287. #ifdef sparc
  288.       expand_asm_operands (build_string (32, "! end of vtable initialization"), 0, 0, 0, 0, input_filename, lineno);
  289. #endif
  290.       expand_end_cond ();
  291.     }
  292.     }
  293.   current_base_init_list = NULL_TREE;
  294.  
  295.   /* Now, perform default initialization of all base classes which
  296.      have not yet been initialized, and unmark baseclasses which
  297.      have been initialized.  */
  298.   for (i = 1; i <= n_baseclasses; i++)
  299.     {
  300.       tree base = current_class_decl;
  301.  
  302.       basetype = CLASSTYPE_BASECLASS (t, i);
  303.       if (! TREE_VIA_VIRTUAL (basetype)
  304.       && ! CLASSTYPE_MARKED6 (basetype))
  305.     {
  306.       if (TYPE_NEEDS_CONSTRUCTING (basetype))
  307.         {
  308.           tree ref;
  309.  
  310.           if (CLASSTYPE_OFFSET (basetype) == integer_zero_node)
  311.         base = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), current_class_decl);
  312.           else
  313.         base = build (PLUS_EXPR, TYPE_POINTER_TO (basetype), current_class_decl, CLASSTYPE_OFFSET (basetype));
  314.  
  315.           ref = build_indirect_ref (base, 0);
  316.           expand_aggr_init_1 (t, 0, ref, NULL_TREE,
  317.                   ! DECL_OFFSET (TYPE_NAME (basetype)),
  318.                   LOOKUP_PROTECTED_OK|LOOKUP_COMPLAIN);
  319.         }
  320.     }
  321.       CLASSTYPE_MARKED6 (basetype) = 0;
  322.  
  323.       if (DECL_OFFSET (TYPE_NAME (basetype)))
  324.     {
  325.       tree assoc;
  326.  
  327.       /* If the vtable installed by the constructor was not
  328.          the right one, fix that here.  */
  329.       if (TREE_VIA_VIRTUAL (basetype))
  330.         assoc = value_member (TYPE_MAIN_VARIANT (basetype),
  331.                   CLASSTYPE_VBASECLASSES (t));
  332.       else
  333.         assoc = assoc_value (TYPE_MAIN_VARIANT (basetype), t);
  334.       if (ASSOC_VTABLE (assoc) != CLASS_ASSOC_VTABLE (basetype))
  335.         expand_expr_stmt (build_virtual_init (t, basetype, base));
  336.     }
  337.     }
  338.  
  339.   if (TYPE_VIRTUAL_P (t))
  340.     {
  341.       if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (t))
  342.     expand_expr_stmt (build_virtual_init (t, t, current_class_decl));
  343.     }
  344. #ifdef SOS
  345.   else if (TYPE_DYNAMIC (t) && CLASSTYPE_NEEDS_VIRTUAL_REINIT (t))
  346.     expand_expr_stmt (build_virtual_init (t, t, current_class_decl));
  347. #endif
  348.  
  349.   /* Members we through expand_member_init.  We initialize all the members
  350.      needing initialization that did not get it so far.  */
  351.   for (; current_member_init_list;
  352.        current_member_init_list = TREE_CHAIN (current_member_init_list))
  353.     {
  354.       tree name = TREE_PURPOSE (current_member_init_list);
  355.       tree init = TREE_VALUE (current_member_init_list);
  356.       tree field = (TREE_CODE (name) == COMPONENT_REF
  357.             ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name));
  358.       tree type;
  359.       
  360.       /* If one member shadows another, get the outermost one.  */
  361.       if (TREE_CODE (field) == TREE_LIST)
  362.     {
  363.       field = TREE_VALUE (field);
  364.       if (decl_type_context (field) != current_class_type)
  365.         error ("field `%s' not in immediate context");
  366.     }
  367.  
  368.       type = TREE_TYPE (field);
  369.  
  370.       if (TREE_STATIC (field))
  371.     {
  372.       error_with_aggr_type (DECL_FIELD_CONTEXT (field),
  373.                 "field `%s::%s' is static; only point of initialization is its declaration", IDENTIFIER_POINTER (name));
  374.       continue;
  375.     }
  376.  
  377.       if (DECL_NAME (field))
  378.     {
  379.       if (TYPE_HAS_CONSTRUCTOR (field))
  380.         error ("multiple initializations given for member `%s'",
  381.            IDENTIFIER_POINTER (DECL_NAME (field)));
  382.  
  383.       if (assigns_this_p && TREE_USED (field))
  384.         error ("field `%s' used before initialized (after assignment to `this')",
  385.            IDENTIFIER_POINTER (DECL_NAME (field)));
  386.     }
  387.  
  388.       /* Mark this node as having been initialized.  */
  389.       TYPE_HAS_CONSTRUCTOR (field) = 1;
  390.       if (DECL_FIELD_CONTEXT (field) != t)
  391.     fields_to_unmark = tree_cons (NULL_TREE, field, fields_to_unmark);
  392.  
  393.       if (TREE_CODE (name) == COMPONENT_REF)
  394.     {
  395.       /* Initialization of anonymous union.  */
  396.       expand_assignment (name, init, 0, 0);
  397.       continue;
  398.     }
  399.       decl = build_component_ref (C_C_D, name, 0, 1);
  400.  
  401.       if (TYPE_NEEDS_CONSTRUCTING (type))
  402.     {
  403.       if (TREE_CODE (type) == ARRAY_TYPE
  404.           && TREE_CHAIN (init) == NULL_TREE
  405.           && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
  406.         {
  407.           /* Initialization of one array from another.  */
  408.           expand_vec_init (TREE_OPERAND (decl, 1), decl,
  409.                    array_type_nelts (type), TREE_VALUE (init), 1);
  410.         }
  411.       else
  412.         expand_aggr_init (decl, init, 0);
  413.     }
  414.       else
  415.     {
  416.       if (init == NULL_TREE)
  417.         {
  418.           error ("types without constructors must have complete initializers");
  419.           init = error_mark_node;
  420.         }
  421.       else if (TREE_CHAIN (init))
  422.         {
  423.           warning ("initializer list treated as compound expression");
  424.           init = build_compound_expr (init);
  425.         }
  426.       else
  427.         init = TREE_VALUE (init);
  428.  
  429.       expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
  430.     }
  431.     }
  432.  
  433.   for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
  434.     {
  435.       /* All we care about is this unique member.  It contains
  436.      all the information we need to know, and that right early.  */
  437.       tree type = TREE_TYPE (member);
  438.       tree init = TYPE_HAS_CONSTRUCTOR (member)
  439.     ? error_mark_node : DECL_INITIAL (member);
  440.  
  441.       /* Unmark this field.  If it is from an anonymous union,
  442.          then unmark the field recursively.  */
  443.       TYPE_HAS_CONSTRUCTOR (member) = 0;
  444.       if (TREE_ANON_UNION_ELEM (member))
  445.     finish_base_init (TREE_TYPE (member), 0);
  446.  
  447.       /* Member had explicit initializer.  */
  448.       if (init == error_mark_node)
  449.     continue;
  450.  
  451.       if (TREE_CODE (member) != FIELD_DECL)
  452.     continue;
  453.  
  454.       if (type == error_mark_node)
  455.     continue;
  456.  
  457.       if (assigns_this_p
  458.       && TREE_USED (member)
  459.       && (TYPE_NEEDS_CONSTRUCTING (type) || init))
  460.     error ("member `%s' used before initialized (after assignment to `this')",
  461.            IDENTIFIER_POINTER (DECL_NAME (member)));
  462.  
  463.       if (TYPE_NEEDS_CONSTRUCTING (type))
  464.     {
  465.       if (init)
  466.         init = build_tree_list (NULL_TREE, init);
  467.       expand_aggr_init (build_component_ref (C_C_D, DECL_NAME (member), 0, 0), init, 0);
  468.     }
  469.       else
  470.     {
  471.       if (init)
  472.         {
  473.           decl = build_component_ref (C_C_D, DECL_NAME (member), 0, 0);
  474.           expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
  475.         }
  476.       else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
  477.         warning ("uninitialized reference member `%s'",
  478.              IDENTIFIER_POINTER (DECL_NAME (member)));
  479.     }
  480.     }
  481.   /* Unmark fields which are initialized for the base class.  */
  482.   while (fields_to_unmark)
  483.     {
  484.       TYPE_HAS_CONSTRUCTOR (TREE_VALUE (fields_to_unmark)) = 0;
  485.       fields_to_unmark = TREE_CHAIN (fields_to_unmark);
  486.     }
  487. }
  488.  
  489. /* This code sets up the virtual function tables appropriate for
  490.    the pointer DECL.  It is a one-ply initialization.
  491.  
  492.    TYPE is the exact type that DECL is supposed to be.  In
  493.    muliple inheritance, this might mean "C's A" if C : A, B.  */
  494. tree
  495. build_virtual_init (for_type, type, decl)
  496.      tree for_type, type;
  497.      tree decl;
  498. {
  499.   tree vtbl, vtbl_ptr;
  500.   tree vtype;
  501.  
  502. #ifdef SOS
  503.   if (TYPE_DYNAMIC (type))
  504.     vtbl = build1 (NOP_EXPR, ptr_type_node, lookup_name (get_identifier (AUTO_VTABLE_NAME)));
  505.   else
  506. #endif
  507.     {
  508. #if 1
  509.       vtbl = ASSOC_VTABLE (assoc_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)),
  510.                     for_type));
  511. #else
  512.       assert (for_type == type);
  513.       vtbl = CLASS_ASSOC_VTABLE (for_type);
  514. #endif /* 1 */
  515.       TREE_USED (vtbl) = 1;
  516.       vtbl = build1 (ADDR_EXPR, TYPE_POINTER_TO (TREE_TYPE (vtbl)), vtbl);
  517.     }
  518.   vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type));
  519.   decl = convert_pointer_to (vtype, decl);
  520.   vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, 0), vtype);
  521.  
  522.   return build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl);
  523. }
  524.  
  525. /* Subroutine of `expand_aggr_vbase_init'.
  526.    BASETYPE is type that is being initialized.  INIT_LIST
  527.    is the list of initializers for the virtual baseclass.  */
  528. static void
  529. expand_aggr_vbase_init_1 (basetype, exp, addr, init_list)
  530.      tree basetype, init_list;
  531. {
  532.   tree init = value_member (basetype, init_list);
  533.   tree ref = build_indirect_ref (addr, 0);
  534.   if (init)
  535.     init = TREE_PURPOSE (init);
  536.   /* Call constructors, but don't set up vtables.  */
  537.   expand_aggr_init_1 (basetype, exp, ref, init, 0,
  538.               LOOKUP_PROTECTED_OK|LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY);
  539.   CLASSTYPE_MARKED6 (basetype) = 0;
  540. }
  541.  
  542. /* Initialize this object's virtual base class pointers.  This must be
  543.    done only at the top-level of the object being constructed.
  544.  
  545.    INIT_LIST is list of initialization for constructor to perform.  */
  546. void
  547. expand_aggr_vbase_init (type, exp, addr, init_list)
  548.      tree type;
  549.      tree exp;
  550.      tree addr;
  551.      tree init_list;
  552. {
  553.   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  554.     {
  555.       tree result = init_vbase_pointers (type, addr);
  556.       tree basetype, vbases;
  557.  
  558.       if (result)
  559.     expand_expr_stmt (build_compound_expr (result));
  560.  
  561.       /* Mark everything as having an initializer
  562.      (either explicit or default).  */
  563.       for (vbases = CLASSTYPE_VBASECLASSES (type);
  564.        vbases; vbases = TREE_CHAIN (vbases))
  565.     CLASSTYPE_MARKED6 (TREE_TYPE (vbases)) = 1;
  566.  
  567.       /* First, initialize baseclasses which could be baseclasses
  568.      for other virtual baseclasses.  */
  569.       for (vbases = CLASSTYPE_VBASECLASSES (type);
  570.        vbases; vbases = TREE_CHAIN (vbases))
  571.     /* Don't initialize twice.  */
  572.     if (CLASSTYPE_MARKED6 (TREE_TYPE (vbases)))
  573.       {
  574.         tree assoc = purpose_member (TREE_TYPE (vbases), result);
  575.         assert (assoc != NULL_TREE);
  576.         expand_aggr_vbase_init_1 (TREE_PURPOSE (assoc), exp,
  577.                       TREE_OPERAND (TREE_VALUE (assoc), 0),
  578.                       init_list);
  579.       }
  580.  
  581.       /* Now initialize the baseclasses which don't have virtual baseclasses.  */
  582.       for (; result; result = TREE_CHAIN (result))
  583.     /* Don't initialize twice.  */
  584.     if (CLASSTYPE_MARKED6 (TREE_PURPOSE (result)))
  585.       {
  586.         abort ();
  587.         expand_aggr_vbase_init_1 (TREE_PURPOSE (result), exp,
  588.                       TREE_OPERAND (TREE_VALUE (result), 0),
  589.                       init_list);
  590.       }
  591.  
  592. #ifdef sparc
  593.       expand_asm_operands (build_string (30, "! end of vbase initialization"), 0, 0, 0, 0, input_filename, lineno);
  594. #endif
  595.     }
  596. }
  597.  
  598. /* Function to give error message if member initialization specification
  599.    is erroneous.  FIELD is the member we decided to initialize.
  600.    TYPE is the type for which the initialization is being performed.
  601.    FIELD must be a member of TYPE, or the base type from which FIELD
  602.    comes must not need a constructor.
  603.    
  604.    MEMBER_NAME is the name of the member.  */
  605.  
  606. static int
  607. member_init_ok_or_else (field, type, member_name)
  608.      tree field;
  609.      tree type;
  610.      char *member_name;
  611. {
  612.   if (field == error_mark_node) return 0;
  613.   if (field == NULL_TREE)
  614.     {
  615.       error_with_aggr_type (type, "class `%s' does not have any field named `%s'",
  616.                 member_name);
  617.       return 0;
  618.     }
  619.   if (DECL_CONTEXT (field) != type
  620.       && TYPE_NEEDS_CONSTRUCTOR (DECL_CONTEXT (field)))
  621.     {
  622.       error ("member `%s' comes from base class needing constructor", member_name);
  623.       return 0;
  624.     }
  625.   return 1;
  626. }
  627.  
  628. /* If NAME is a viable field name for the aggregate DECL,
  629.    and PARMS is a viable parameter list, then expand an _EXPR
  630.    which describes this initialization.
  631.  
  632.    Note that we do not need to chase through the class's base classes
  633.    to look for NAME, because if it's in that list, it will be handled
  634.    by the constructor for that base class.
  635.  
  636.    We do not yet have a fixed-point finder to instantiate types
  637.    being fed to overloaded constructors.  If there is a unique
  638.    constructor, then argument types can be got from that one.
  639.  
  640.    If INIT is non-NULL, then it the initialization should
  641.    be placed in `current_base_init_list', where it will be processed
  642.    by `finish_base_init'.  */
  643. void
  644. expand_member_init (exp, name, init)
  645.      tree exp, name, init;
  646. {
  647.   extern tree ptr_type_node;    /* should be in tree.h */
  648.  
  649.   tree basetype = NULL_TREE, field;
  650.   tree parm;
  651.   tree rval, type;
  652.   tree actual_name;
  653.  
  654.   if (exp == NULL_TREE)
  655.     return;            /* complain about this later */
  656.  
  657.   type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
  658.  
  659.   if (name == NULL_TREE && IS_AGGR_TYPE (type))
  660.     switch (CLASSTYPE_N_BASECLASSES (type))
  661.       {
  662.       case 0:
  663.     error ("base class initializer specified, but no base class to initialize");
  664.     return;
  665.       case 1:
  666.     basetype = CLASSTYPE_BASECLASS (type, 1);
  667.     break;
  668.       default:
  669.     error ("initializer for unnamed base class ambiguous");
  670.     error_with_aggr_type (type, "(type `%s' uses multiple inheritance)");
  671.     return;
  672.       }
  673.  
  674.   if (init)
  675.     {
  676.       /* The grammar should not allow fields which have names
  677.      that are TYPENAMEs.  Therefore, if the field has
  678.      a non-NULL TREE_TYPE, we may assume that this is an
  679.      attempt to initialize a base class member of the current
  680.      type.  Otherwise, it is an attempt to initialize a
  681.      member field.  */
  682.  
  683.       if (init == void_type_node)
  684.     init = NULL_TREE;
  685.  
  686.       if (name == NULL_TREE || TREE_TYPE (name))
  687.     {
  688.       tree base_init;
  689.  
  690.       if (name == NULL_TREE)
  691.         if (basetype)
  692.           name = DECL_NAME (TYPE_NAME (basetype));
  693.         else
  694.           {
  695.         error ("no base class to initialize");
  696.         return;
  697.           }
  698.       else
  699.         {
  700.           basetype = TREE_TYPE (TREE_TYPE (name));
  701.           if (basetype != type
  702.           && ! value_member (basetype, TYPE_BASETYPES (type))
  703.           && ! value_member (basetype, CLASSTYPE_VBASECLASSES (type)))
  704.         {
  705.           if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  706.             error ("type `%s' is not an immediate or virtual basetype for `%s'",
  707.                IDENTIFIER_POINTER (name),
  708.                TYPE_NAME_STRING (type));
  709.           else
  710.             error ("type `%s' is not an immediate for `%s'",
  711.                IDENTIFIER_POINTER (name),
  712.                TYPE_NAME_STRING (type));
  713.           return;
  714.         }
  715.         }
  716.  
  717.       if (purpose_member (name, current_base_init_list))
  718.         {
  719.           error ("base class `%s' already initialized",
  720.              IDENTIFIER_POINTER (name));
  721.           return;
  722.         }
  723.  
  724.       base_init = build_tree_list (name, init);
  725.       TREE_TYPE (base_init) = basetype;
  726.       current_base_init_list = chainon (current_base_init_list, base_init);
  727.     }
  728.       else
  729.     {
  730.       tree member_init;
  731.  
  732.       field = lookup_field (type, name, 1);
  733.  
  734.       if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
  735.         return;
  736.  
  737.       if (purpose_member (name, current_member_init_list))
  738.         {
  739.           error ("field `%s' already initialized", DECL_NAME (name));
  740.           return;
  741.         }
  742.  
  743.       member_init = build_tree_list (name, init);
  744.       TREE_TYPE (member_init) = TREE_TYPE (field);
  745.       current_member_init_list = chainon (current_member_init_list, member_init);
  746.     }
  747.       return;
  748.     }
  749.   else if (name == NULL_TREE)
  750.     {
  751.       compiler_error ("expand_member_init: name == NULL_TREE");
  752.       return;
  753.     }
  754.  
  755.   basetype = type;
  756.   field = lookup_field (basetype, name, 0);
  757.  
  758.   if (! member_init_ok_or_else (field, basetype, IDENTIFIER_POINTER (name)))
  759.     return;
  760.  
  761.   /* now see if there is a constructor for this type
  762.      which will take these args. */
  763.  
  764.   if (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (field)))
  765.     {
  766.       tree parmtypes, fndecl;
  767.  
  768.       if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
  769.     {
  770.       /* just know that we've seen something for this node */
  771.       DECL_INITIAL (exp) = error_mark_node;
  772.       TREE_USED (exp) = 1;
  773.     }
  774.       type = TYPE_MAIN_VARIANT (TREE_TYPE (field));
  775.       actual_name = DECL_NAME (TYPE_NAME (type));
  776.       parm = build_component_ref (exp, name, 0, 0);
  777.  
  778.       /* Now get to the constructor.  */
  779.       field = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
  780.       /* Get past destructor, if any.  */
  781.       if (TYPE_HAS_DESTRUCTOR (type))
  782.     field = TREE_CHAIN (field);
  783.  
  784.       /* If the field is unique, we can use the parameter
  785.      types to guide possible type instantiation.  */
  786.       if (TREE_CHAIN (field) == NULL_TREE)
  787.     {
  788.       fndecl = TREE_TYPE (field);
  789.       parmtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
  790.     }
  791.       else
  792.     {
  793.       parmtypes = NULL_TREE;
  794.       fndecl = NULL_TREE;
  795.     }
  796.  
  797.       init = actualparameterlist (parm, parmtypes, NULL_TREE, fndecl, LOOKUP_NORMAL);
  798.       if (init == NULL_TREE || TREE_TYPE (init) != error_mark_node)
  799.     rval = build_method_call (NULL_TREE, actual_name, init, NULL_TREE, LOOKUP_NORMAL);
  800.       else
  801.     return;
  802.  
  803.       if (rval != error_mark_node)
  804.     {
  805.       /* Now, fill in the first parm with our guy */
  806.       TREE_VALUE (TREE_OPERAND (rval, 1))
  807.         = build_unary_op (ADDR_EXPR, parm, 0);
  808.       TREE_TYPE (rval) = ptr_type_node;
  809.       TREE_VOLATILE (rval) = 1;
  810.     }
  811.     }
  812.   else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
  813.     {
  814.       parm = build_component_ref (exp, name, 0, 0);
  815.       expand_aggr_init (parm, NULL_TREE, 0);
  816.       rval = error_mark_node;
  817.     }
  818.  
  819.   /* Now initialize the member.  It does not have to
  820.      be of aggregate type to receive initialization.  */
  821.   if (rval != error_mark_node)
  822.     expand_expr_stmt (rval);
  823. }
  824.  
  825. /* This is like `expand_member_init', only it stores one aggregate
  826.    value into another.
  827.  
  828.    INIT comes in two flavors: it is either a value which
  829.    is to be stored in EXP, or it is a parameter list
  830.    to go to a constructor, which will operate on EXP.
  831.    If `init' is a CONSTRUCTOR, then we emit a warning message,
  832.    explaining that such initializaitions are illegal.
  833.  
  834.    ALIAS_THIS is nonzero iff we are initializing something which is
  835.    essentially an alias for C_C_D.  In this case, the base constructor
  836.    may move it on us, and we must keep track of such deviations.
  837.  
  838.    If INIT resolves to a CALL_EXPR which happens to return
  839.    something of the type we are looking for, then we know
  840.    that we can safely use that call to perform the
  841.    initialization.
  842.  
  843.    The virtual function table pointer cannot be set up here, because
  844.    we do not really know its type.
  845.  
  846.    Virtual baseclass pointers are also set up here.
  847.  
  848.    This never calls operator=().
  849.  
  850.    When initializing, nothing is CONST.  */
  851.  
  852. void
  853. expand_aggr_init (exp, init, alias_this)
  854.      tree exp, init;
  855.      int alias_this;
  856. {
  857.   tree type = TREE_TYPE (exp);
  858.   tree addr;
  859.   int was_const = TREE_READONLY (exp);
  860.  
  861.   if (init == error_mark_node)
  862.     return;
  863.  
  864.   TREE_READONLY (exp) = 0;
  865.  
  866.   if (TREE_CODE (type) == ARRAY_TYPE)
  867.     {
  868.       /* Must arrange to initialize each element of EXP
  869.      from elements of INIT.  */
  870.       int was_const_elts = TREE_READONLY (TREE_TYPE (type));
  871.       tree itype = init ? TREE_TYPE (init) : NULL_TREE;
  872.       if (was_const_elts)
  873.     {
  874.       tree atype = build_cplus_array_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
  875.                            TYPE_DOMAIN (type));
  876.       if (TREE_TYPE (exp) == TREE_TYPE (init))
  877.         TREE_TYPE (init) = atype;
  878.       TREE_TYPE (exp) = atype;
  879.     }
  880.       expand_vec_init (exp, exp, array_type_nelts (type), init,
  881.                init && TREE_TYPE (init) == TREE_TYPE (exp));
  882.       TREE_READONLY (exp) = was_const;
  883.       TREE_TYPE (exp) = type;
  884.       if (init) TREE_TYPE (init) = itype;
  885.       return;
  886.     }
  887.  
  888.   if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
  889.     /* just know that we've seen something for this node */
  890.     TREE_USED (exp) = 1;
  891.  
  892.   /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the
  893.      constructor as parameters to an implicit GNU C++ constructor.  */
  894.   if (init && TREE_CODE (init) == CONSTRUCTOR
  895. #if 0
  896.       && ! TREE_HAS_CONSTRUCTOR (init)
  897. #endif
  898.       && TREE_TYPE (init) == type)
  899.     init = CONSTRUCTOR_ELTS (init);
  900.   expand_aggr_init_1 (type, exp, exp, init, alias_this, LOOKUP_NORMAL);
  901.   TREE_READONLY (exp) = was_const;
  902. }
  903.  
  904. /* This function is responsible for initializing EXP with INIT
  905.    (if any).
  906.  
  907.    FOR_TYPE is the type for who we are performing the initialization.
  908.    For example, if W is a virtual base class of A and B, and C : A, B
  909.    if we are initializing B, then W must contain B's W vtable, whereas
  910.    were we initializing C, W must contain C's W vtable.
  911.  
  912.    TRUE_EXP is nonzero if it is the true expression being initialized.
  913.    In this case, it may be EXP, or may just contain EXP.  The reason we
  914.    need this is because if EXP is a base element of TRUE_EXP, we
  915.    don't necessarily know by looking at EXP where its virtual
  916.    baseclass fields should really be pointing.  But we do know
  917.    from TRUE_EXP.  In constructors, we don't know anything about
  918.    the value being initialized.
  919.  
  920.    ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
  921.  
  922.    FLAGS is just passes to `build_method_call'.  See that function for
  923.    its description.  */
  924.  
  925. static void
  926. expand_aggr_init_1 (for_type, true_exp, exp, init, alias_this, flags)
  927.      tree for_type;
  928.      tree true_exp, exp;
  929.      tree init;
  930.      int alias_this;
  931.      int flags;
  932. {
  933.   tree type = TREE_TYPE (exp);
  934.   tree init_type = NULL_TREE;
  935.   tree rval;
  936.  
  937.   assert (init != error_mark_node && type != error_mark_node);
  938.  
  939.   /* Use a function returning the desired type to initialize EXP for us.
  940.      If the function is a constructor, and its first argument is
  941.      NULL_TREE, know that it was meant for us--just slide exp on
  942.      in and expand the constructor.  Constructors now come
  943.      as NEW_EXPRs.  */
  944.   if (init)
  945.     {
  946.       tree init_list = NULL_TREE;
  947.  
  948.       if (TREE_CODE (init) == TREE_LIST)
  949.     {
  950.       init_list = init;
  951.       if (TREE_CHAIN (init) == NULL_TREE)
  952.         init = TREE_VALUE (init);
  953.     }
  954.  
  955.       init_type = TREE_TYPE (init);
  956.  
  957.       if (TREE_CODE (init) != TREE_LIST)
  958.     {
  959.       if (TREE_CODE (init_type) == ERROR_MARK)
  960.         return;
  961.  
  962. #if 0
  963.       /* These lines are found troublesome 5/11/89.  */
  964.       if (TREE_CODE (init_type) == REFERENCE_TYPE)
  965.         init_type = TREE_TYPE (init_type);
  966. #endif
  967.  
  968.       /* This happens when we use C++'s functional cast notation
  969.          to act as the initializer for something not of that same
  970.          type.  In that case, we need to create the initializer
  971.          separately from the object being initialized.  */
  972.       if (TREE_CODE (init) == NEW_EXPR && init_type != type)
  973.         {
  974.           init = TREE_OPERAND (init, 1);
  975.           init = build (CALL_EXPR, init_type,
  976.                 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
  977.           TREE_VOLATILE (init) = 1;
  978. #if 0
  979.           TREE_RAISES (init) = ??
  980. #endif
  981.           if (init_list)
  982.         TREE_VALUE (init_list) = init;
  983.         }
  984.  
  985.       if (init_type == type && TREE_CODE (init) == CALL_EXPR
  986. #if 0
  987.           /* It is legal to directly initialize from a CALL_EXPR
  988.          without going through X(X&), apparently.  */
  989.           && ! TYPE_GETS_INIT_REF (type)
  990. #endif
  991.           )
  992.         {
  993.           /* A CALL_EXPR is a legitmate form of initialization, so
  994.          we should not print this warning message.  */
  995. #if 0
  996.           /* Should have gone away due to 5/11/89 change.  */
  997.           if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
  998.         init = convert_from_reference (init);
  999. #endif
  1000. #if 0
  1001.           if (TYPE_GETS_ASSIGNMENT (type))
  1002.         warning ("bitwise copy: `%s' has a member with operator=()",
  1003.              TYPE_NAME_STRING (type));
  1004. #endif
  1005.           expand_assignment (exp, init, 0, 0);
  1006.           if (exp == DECL_RESULT (current_function_decl))
  1007.         {
  1008.           /* Failing this assertion means that the return value
  1009.              from receives multiple initializations.  */
  1010.           assert (DECL_INITIAL (exp) == NULL_TREE || DECL_INITIAL (exp) == error_mark_node);
  1011.           DECL_INITIAL (exp) = init;
  1012.         }
  1013.           return;
  1014.         }
  1015.       else if (init_type == type
  1016.            && TREE_CODE (init) == COND_EXPR)
  1017.         {
  1018.           /* Push value to be initialized into the cond, where possible.
  1019.              Avoid spurious warning messages when initializing the
  1020.          result of this function.  */
  1021.           TREE_OPERAND (init, 1)
  1022.         = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 1));
  1023.           if (exp == DECL_RESULT (current_function_decl))
  1024.         DECL_INITIAL (exp) = NULL_TREE;
  1025.           TREE_OPERAND (init, 2)
  1026.         = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 2));
  1027.           if (exp == DECL_RESULT (current_function_decl))
  1028.         DECL_INITIAL (exp) = init;
  1029.           expand_expr (init, const0_rtx, VOIDmode, 0);
  1030.           return;
  1031.         }
  1032.     }
  1033.  
  1034.       /* We did not know what we were initializing before.  Now we do.  */
  1035.       if (TREE_CODE (init) == NEW_EXPR)
  1036.     {
  1037.       tree tmp = TREE_OPERAND (TREE_OPERAND (init, 1), 1);
  1038.  
  1039.       assert (tmp != NULL_TREE);
  1040.  
  1041.       if ((TREE_CODE (TREE_VALUE (tmp)) == NOP_EXPR
  1042.            && TREE_OPERAND (TREE_VALUE (tmp), 0) == integer_zero_node)
  1043.           || (0 && !flag_this_is_variable
  1044.           && TREE_CALLS_NEW (TREE_VALUE (tmp))))
  1045.         {
  1046.           /* In order for this to work for RESULT_DECLs, if their
  1047.          type has a constructor, then they must be BLKmode
  1048.          so that they will be meaningfully addressable.  */
  1049.           tree arg = build_unary_op (ADDR_EXPR, exp, 0);
  1050.           init = TREE_OPERAND (init, 1);
  1051.           init = build (CALL_EXPR, build_pointer_type (TREE_TYPE (init)),
  1052.                 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
  1053.           TREE_VOLATILE (init) = 1;
  1054. #if 0
  1055.           TREE_RAISES (init) = ??
  1056. #endif
  1057.           TREE_VALUE (TREE_OPERAND (init, 1))
  1058.         = convert_pointer_to (TREE_TYPE (TREE_TYPE (TREE_VALUE (tmp))), arg);
  1059.  
  1060.           if (alias_this)
  1061.         {
  1062.           expand_assignment (current_function_decl, init, 0, 0);
  1063.           return;
  1064.         }
  1065.           if (exp == DECL_RESULT (current_function_decl))
  1066.         {
  1067.           if (DECL_INITIAL (DECL_RESULT (current_function_decl)))
  1068.             fatal ("return value from function receives multiple initializations");
  1069.           DECL_INITIAL (exp) = init;
  1070.         }
  1071.           expand_expr_stmt (init);
  1072.           return;
  1073.         }
  1074.     }
  1075.  
  1076.       /* Handle this case: when calling a constructor: xyzzy foo(bar);
  1077.      which really means:  xyzzy foo = bar; Ugh!
  1078.  
  1079.      We can also be called with an initializer for an object
  1080.      which has virtual functions, but no constructors.  In that
  1081.      case, we perform the assignment first, then initialize
  1082.      the virtual function table pointer fields.  */
  1083.  
  1084.       if (! TYPE_NEEDS_CONSTRUCTING (type))
  1085.     {
  1086.       if (init_list && TREE_CHAIN (init_list))
  1087.         {
  1088.           warning ("initializer list being treated as compound expression");
  1089.           init = convert (TREE_TYPE (exp), build_compound_expr (init_list));
  1090.           if (init == error_mark_node)
  1091.         return;
  1092.         }
  1093.       if (TREE_CODE (exp) == VAR_DECL
  1094.           && TREE_CODE (init) == CONSTRUCTOR
  1095.           && TREE_HAS_CONSTRUCTOR (init))
  1096.         store_init_value (exp, init);
  1097.       else
  1098.         expand_assignment (exp, init, 0, 0);
  1099.  
  1100.       if (TYPE_VIRTUAL_P (type))
  1101.         expand_recursive_init (for_type, true_exp, exp, init, CLASSTYPE_BASE_INIT_LIST (type), alias_this);
  1102.       return;
  1103.     }
  1104.  
  1105.       /* See whether we can go through a type conversion operator.
  1106.      This wins over going through a constructor because we may be
  1107.      able to avoid an X(X&) constructor.  */
  1108.       if (TREE_CODE (init) != TREE_LIST)
  1109.     {
  1110.       tree ttype = TREE_CODE (init_type) == REFERENCE_TYPE
  1111.         ? TREE_TYPE (init_type) : init_type;
  1112.  
  1113.       if (ttype != type && IS_AGGR_TYPE (ttype))
  1114.         {
  1115.           tree rval = build_type_conversion (CONVERT_EXPR, type, init, 0);
  1116.  
  1117.           if (rval)
  1118.         {
  1119.           expand_assignment (exp, rval, 0, 0);
  1120.           return;
  1121.         }
  1122.         }
  1123.     }
  1124.     }
  1125.  
  1126.   if (TYPE_HAS_CONSTRUCTOR (type))
  1127.     {
  1128.       /* It fails because there may not be a constructor which takes
  1129.      its own type as the first (or only parameter), but which does
  1130.      take other types via a conversion.  So, if the thing initializing
  1131.      the expression is a unit element of type X, first try X(X&),
  1132.      followed by initialization by X.  If neither of these work
  1133.      out, then look hard.  */
  1134.       tree parms = (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
  1135.     ? init : build_tree_list (NULL_TREE, init);
  1136.       int xxref_init_possible;
  1137.  
  1138.       if (parms) init = TREE_VALUE (parms);
  1139.       xxref_init_possible = init && TREE_CHAIN (parms) == NULL_TREE
  1140.     ? LOOKUP_SPECULATIVELY : 0;
  1141.  
  1142.       if (TYPE_HAS_INIT_REF (type))
  1143.     xxref_init_possible = 0;
  1144.  
  1145.       if (TYPE_USES_VIRTUAL_BASECLASSES (type))
  1146.     {
  1147.       assert (TYPE_USES_VIRTUAL_BASECLASSES (for_type));
  1148.       if (true_exp == exp)
  1149.         parms = tree_cons (NULL_TREE, integer_one_node, parms);
  1150.       else
  1151.         parms = tree_cons (NULL_TREE, integer_zero_node, parms);
  1152.       flags |= LOOKUP_HAS_IN_CHARGE;
  1153.     }
  1154.       rval = build_method_call (exp, DECL_NAME (TYPE_NAME (type)), parms,
  1155.                 CLASSTYPE_AS_LIST (for_type), flags|xxref_init_possible);
  1156.       if (rval == NULL_TREE && xxref_init_possible)
  1157.     {
  1158.       tree init_type = TREE_TYPE (init);
  1159.       if (TREE_CODE (init_type) == REFERENCE_TYPE)
  1160.         init_type = TREE_TYPE (init_type);
  1161.       if (TYPE_MAIN_VARIANT (init_type) == TYPE_MAIN_VARIANT (type)
  1162.           || (IS_AGGR_TYPE (init_type)
  1163.           && get_base_type (type, init_type, 0)))
  1164.         {
  1165.           if (type == for_type
  1166.           && TYPE_USES_VIRTUAL_BASECLASSES (type))
  1167.         {
  1168.           tree addr = build_unary_op (ADDR_EXPR, exp, 0);
  1169.           expand_aggr_vbase_init (type, exp, addr, NULL_TREE);
  1170.  
  1171.           expand_expr_stmt (build_vbase_vtables_init (type, type, exp, addr));
  1172. #ifdef sparc
  1173.           expand_asm_operands (build_string (32, "! end of vtable initialization"), 0, 0, 0, 0, input_filename, lineno);
  1174. #endif
  1175.         }
  1176.           expand_expr_stmt (build_modify_expr (exp, INIT_EXPR, init));
  1177.           return;
  1178.         }
  1179.       else
  1180.         rval = build_method_call (exp, DECL_NAME (TYPE_NAME (type)), parms,
  1181.                       CLASSTYPE_AS_LIST (for_type), flags);
  1182.     }
  1183.  
  1184.       /* Private, protected, or otherwise unavailable.  */
  1185.       if (rval == error_mark_node && (flags&LOOKUP_COMPLAIN))
  1186.     error_with_aggr_type (for_type, "in base initialization for class `%s'");
  1187.       /* A valid initialization using constructor.  */
  1188.       else if (rval != error_mark_node && rval != NULL_TREE)
  1189.     {
  1190.       /* p. 222: if the base class assigns to `this', then that
  1191.          value is used in the derived class.  */
  1192.       if (flag_this_is_variable && alias_this)
  1193.         {
  1194.           TREE_TYPE (rval) = TREE_TYPE (current_class_decl);
  1195.           expand_assignment (current_class_decl, rval, 0, 0);
  1196.         }
  1197.       else
  1198.         expand_expr_stmt (rval);
  1199.     }
  1200.       else if (parms && TREE_CHAIN (parms) == NULL_TREE)
  1201.     {
  1202.       /* If we are initializing one aggregate value
  1203.          from another, and though there are constructors,
  1204.          and none accept the initializer, just do a bitwise
  1205.          copy.
  1206.          
  1207.          @@ This should reject initializer which a constructor
  1208.          @@ rejected on visibility gounds, but there is
  1209.          @@ no way right now to recognize that case with
  1210.          @@ just `error_mark_node'.  */
  1211.       tree itype;
  1212.       init = TREE_VALUE (parms);
  1213.       itype = TREE_TYPE (init);
  1214.       if (TREE_CODE (itype) == REFERENCE_TYPE)
  1215.         {
  1216.           init = convert_from_reference (init);
  1217.           itype = TREE_TYPE (init);
  1218.         }
  1219.       itype = TYPE_MAIN_VARIANT (itype);
  1220.       if (comptypes (TYPE_MAIN_VARIANT (type), itype, 0))
  1221.         {
  1222.           warning ("bitwise copy in initialization of type `%s'",
  1223.                TYPE_NAME_STRING (type));
  1224.           rval = build (INIT_EXPR, type, exp, init);
  1225.           expand_expr_stmt (rval);
  1226.         }
  1227.       else
  1228.         {
  1229.           error_with_aggr_type (for_type, "in base initialization for class `%s',");
  1230.           error_with_aggr_type (type, "invalid initializer to constructor for type `%s'");
  1231.           return;
  1232.         }
  1233.     }
  1234.       else
  1235.     {
  1236.       if (init == NULL_TREE)
  1237.         assert (parms == NULL_TREE);
  1238.       if (parms == NULL_TREE && TREE_VIA_VIRTUAL (for_type))
  1239.         error_with_aggr_type (for_type, "virtual baseclass `%s' does not have default initializer");
  1240.       else
  1241.         {
  1242.           error_with_aggr_type (for_type, "in base initialization for class `%s',");
  1243.           /* This will make an error message for us.  */
  1244.           build_method_call (exp, DECL_NAME (TYPE_NAME (type)), parms,
  1245.                  CLASSTYPE_AS_LIST (for_type), LOOKUP_NORMAL);
  1246.         }
  1247.       return;
  1248.     }
  1249.       /* Constructor has been called, but vtables may be for TYPE
  1250.      rather than for FOR_TYPE.  */
  1251.     }
  1252.   else if (TREE_CODE (type) == ARRAY_TYPE)
  1253.     {
  1254.       if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
  1255.     expand_vec_init (exp, exp, array_type_nelts (type), init, 0);
  1256.       else if (TYPE_VIRTUAL_P (TREE_TYPE (type)))
  1257.     sorry ("arrays of objects with virtual functions but no constructors");
  1258.     }
  1259.   else
  1260.     expand_recursive_init (for_type, true_exp, exp, init, CLASSTYPE_BASE_INIT_LIST (type), alias_this);
  1261. }
  1262.  
  1263. /* A pointer which holds the initializer.  First call to
  1264.    expand_aggr_init gets this value pointed to, and sets it to init_null.  */
  1265. static tree *init_ptr, init_null;
  1266.  
  1267. /* Subroutine of expand_recursive_init:
  1268.  
  1269.    ADDR is the address of the expression being initialized.
  1270.    INIT_LIST is the cons-list of initializations to be performed.
  1271.    ALIAS_THIS is its same, lovable self.  */
  1272. static void
  1273. expand_recursive_init_1 (for_type, true_exp, addr, init_list, alias_this)
  1274.      tree for_type, true_exp, addr;
  1275.      tree init_list;
  1276.      int alias_this;
  1277. {
  1278.   while (init_list)
  1279.     {
  1280.       if (TREE_PURPOSE (init_list))
  1281.     {
  1282.       if (TREE_CODE (TREE_PURPOSE (init_list)) == FIELD_DECL)
  1283.         {
  1284.           tree member = TREE_PURPOSE (init_list);
  1285.           tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), 0);
  1286.           tree member_base = build (COMPONENT_REF, TREE_TYPE (member), subexp, member);
  1287.           if (IS_AGGR_TYPE (TREE_TYPE (member)))
  1288.         expand_aggr_init (member_base, DECL_INITIAL (member), 0);
  1289.           else if (TREE_CODE (TREE_TYPE (member)) == ARRAY_TYPE
  1290.                && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (member)))
  1291.         {
  1292.           member_base = save_expr (default_conversion (member_base));
  1293.           expand_vec_init (member, member_base,
  1294.                    array_type_nelts (TREE_TYPE (member)),
  1295.                    DECL_INITIAL (member), 0);
  1296.         }
  1297.           else expand_expr_stmt (build_modify_expr (member_base, INIT_EXPR, DECL_INITIAL (member)));
  1298.         }
  1299.       else if (TREE_CODE (TREE_PURPOSE (init_list)) == TREE_LIST)
  1300.         {
  1301.           expand_recursive_init_1 (for_type, true_exp, addr, TREE_PURPOSE (init_list), alias_this);
  1302.           expand_recursive_init_1 (for_type, true_exp, addr, TREE_VALUE (init_list), alias_this);
  1303.         }
  1304.       else if (TREE_CODE (TREE_PURPOSE (init_list)) == ERROR_MARK)
  1305.         {
  1306.           /* Only initialize the virtual function tables if we
  1307.          are initializing the ultimate users of those vtables.  */
  1308.           if (TREE_VALUE (init_list))
  1309.         {
  1310.           expand_expr_stmt (build_virtual_init (for_type, TREE_VALUE (init_list), addr));
  1311.           if (TREE_VALUE (init_list) == for_type
  1312.               && TYPE_USES_VIRTUAL_BASECLASSES (for_type))
  1313.             expand_expr_stmt (build_vbase_vtables_init (for_type, TREE_VALUE (init_list), true_exp, addr));
  1314. #ifdef sparc
  1315.           expand_asm_operands (build_string (32, "! end of vtable initialization"), 0, 0, 0, 0, input_filename, lineno);
  1316. #endif
  1317.         }
  1318.         }
  1319.       else abort ();
  1320.     }
  1321.       else if (TREE_VALUE (init_list)
  1322.            && TREE_CODE (TREE_VALUE (init_list)) == RECORD_TYPE)
  1323.     {
  1324.       tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), 0);
  1325.       expand_aggr_init_1 (for_type, true_exp, subexp, *init_ptr,
  1326.                   alias_this && ! DECL_OFFSET (TYPE_NAME (TREE_VALUE (init_list))),
  1327.                   LOOKUP_PROTECTED_OK|LOOKUP_COMPLAIN);
  1328.  
  1329.       /* INIT_PTR is used up.  */
  1330.       init_ptr = &init_null;
  1331.     }
  1332.       else
  1333.     abort ();
  1334.       init_list = TREE_CHAIN (init_list);
  1335.     }
  1336. }
  1337.  
  1338. /* Initialize EXP with INIT.  Type EXP does not have a constructor,
  1339.    but it has a baseclass with a constructor or a virtual function
  1340.    table which needs initializing.
  1341.  
  1342.    INIT_LIST is a cons-list describing what parts of EXP actually
  1343.    need to be initialized.  INIT is given to the *unique*, first
  1344.    constructor within INIT_LIST.  If there are multiple first
  1345.    constructors, such as with multiple inheritance, INIT must
  1346.    be zero or an ambiguity error is reported.
  1347.  
  1348.    ALIAS_THIS is passed from `expand_aggr_init'.  See comments
  1349.    there.  */
  1350.  
  1351. static void
  1352. expand_recursive_init (for_type, true_exp, exp, init, init_list, alias_this)
  1353.      tree for_type, true_exp, exp, init;
  1354.      tree init_list;
  1355.      int alias_this;
  1356. {
  1357.   tree *old_init_ptr = init_ptr;
  1358.   tree addr = build_unary_op (ADDR_EXPR, exp, 0);
  1359.   init_ptr = &init;
  1360.  
  1361.   if (true_exp == exp && TYPE_USES_VIRTUAL_BASECLASSES (for_type))
  1362.     {
  1363.       expand_aggr_vbase_init (for_type, exp, addr, init_list);
  1364.       expand_expr_stmt (build_vbase_vtables_init (for_type, for_type, true_exp, addr));
  1365.     }
  1366.   expand_recursive_init_1 (for_type, true_exp, addr, init_list, alias_this);
  1367.  
  1368.   if (*init_ptr)
  1369.     {
  1370.       tree type = TREE_TYPE (exp);
  1371.  
  1372.       if (TREE_CODE (type) == REFERENCE_TYPE)
  1373.     type = TREE_TYPE (type);
  1374.       if (IS_AGGR_TYPE (type))
  1375.     error_with_aggr_type (type, "unexpected argument to constructor `%s'");
  1376.       else
  1377.     error ("unexpected argument to constructor");
  1378.     }
  1379.   init_ptr = old_init_ptr;
  1380. }
  1381.  
  1382. /* Report an error if NAME is not the name of a user-defined,
  1383.    aggregate type.  If OR_ELSE is nonzero, give an error message.  */
  1384. int
  1385. is_aggr_typedef (name, or_else)
  1386.      tree name;
  1387. {
  1388.   tree type = TREE_TYPE (name);
  1389.  
  1390.   if (type == NULL_TREE || TREE_CODE (type) != TYPE_DECL)
  1391.     {
  1392.       if (or_else)
  1393.     error ("`%s' fails to be an aggregate typedef",
  1394.            IDENTIFIER_POINTER (name));
  1395.       return 0;
  1396.     }
  1397.   type = TREE_TYPE (type);
  1398.   if (! IS_AGGR_TYPE (type))
  1399.     {
  1400.       fatal ("type `%s' is of non-aggregate type",
  1401.          IDENTIFIER_POINTER (name));
  1402.       return 0;
  1403.     }
  1404.   return 1;
  1405. }
  1406.  
  1407. /* This code could just as well go in `cplus-class.c', but is placed here for
  1408.    modularity.  */
  1409.  
  1410. /* For an expression of the form CNAME :: NAME (PARMLIST), build
  1411.    the appropriate function call.  */
  1412. tree
  1413. build_member_call (cname, name, parmlist)
  1414.      tree cname, name, parmlist;
  1415. {
  1416.   tree type, t;
  1417.   tree method_name = name;
  1418.   int dtor = 0;
  1419.  
  1420.   if (TREE_CODE (method_name) == BIT_NOT_EXPR)
  1421.     {
  1422.       method_name = TREE_OPERAND (method_name, 0);
  1423.       dtor = 1;
  1424.     }
  1425.  
  1426.   if (TREE_CODE (cname) == SCOPE_REF)
  1427.     {
  1428.       sorry ("multiple scope qualifications in build_member_call");
  1429.       return error_mark_node;
  1430.     }
  1431.  
  1432.   if (! is_aggr_typedef (cname, 1))
  1433.     return error_mark_node;
  1434.  
  1435.   /* An operator we did not like.  */
  1436.   if (name == NULL_TREE)
  1437.     return error_mark_node;
  1438.  
  1439.   if (dtor)
  1440.     {
  1441.       if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (TREE_TYPE (cname))))
  1442.     error ("type `%s' does not have a destructor",
  1443.            IDENTIFIER_POINTER (cname));
  1444.       else
  1445.     error ("destructor specification error");
  1446.       return error_mark_node;
  1447.     }
  1448.  
  1449.   if (TREE_CODE (name) == OP_IDENTIFIER)
  1450.     method_name = build_operator_fnname (&name, parmlist, 1);
  1451.   type = TREE_TYPE (TREE_TYPE (cname));
  1452.   t = lookup_fnfields (CLASSTYPE_AS_LIST (type), method_name, 0);
  1453.   if (t)
  1454.     {
  1455.       /* No object?  Then just fake one up, and let build_method_call
  1456.      figure out what to do.  */
  1457.       int dont_use_this = 0;
  1458.       tree basetype_path, decl;
  1459.  
  1460.       /* Determine whether to use `this' as the base object.  */
  1461.       if (current_class_type == 0
  1462.       || get_base_distance (type, current_class_type, 0, &basetype_path) == -1)
  1463.     dont_use_this = 1;
  1464.  
  1465.       if (dont_use_this)
  1466.     {
  1467.       basetype_path = NULL_TREE;
  1468.       decl = build1 (NOP_EXPR,
  1469.              TYPE_POINTER_TO (TREE_TYPE (TREE_TYPE (cname))),
  1470.              error_mark_node);
  1471.     }
  1472.       else if (current_class_decl == 0)
  1473.     decl = build1 (NOP_EXPR,
  1474.                TYPE_POINTER_TO (TREE_TYPE (TREE_TYPE (cname))),
  1475.                error_mark_node);
  1476.       else decl = current_class_decl;
  1477.  
  1478.       return build_method_call (decl, method_name, parmlist, basetype_path,
  1479.                 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
  1480.     }
  1481.   else
  1482.     {
  1483.       char *err_name;
  1484.       if (TREE_CODE (name) == OP_IDENTIFIER)
  1485.     {
  1486.       char *op_name = operator_name_string (method_name);
  1487.       err_name = (char *)alloca (13 + strlen (op_name));
  1488.       sprintf (err_name, "operator %s", op_name);
  1489.     }
  1490.       else if (TREE_CODE (name) == IDENTIFIER_NODE)
  1491.     err_name = IDENTIFIER_POINTER (name);
  1492.       else
  1493.     abort ();
  1494.  
  1495.       error ("no method `%s::%s'", IDENTIFIER_POINTER (cname), err_name);
  1496.       return error_mark_node;
  1497.     }
  1498. }
  1499.  
  1500. /* Build a reference to a member of an aggregate.  This is not a
  1501.    C++ `&', but really something which can have its address taken,
  1502.    and then act as a pointer to member, for example CNAME :: FIELD
  1503.    can have its address taken by saying & CNAME :: FIELD.
  1504.  
  1505.    @@ Prints out lousy diagnostics for operator <typename>
  1506.    @@ fields.
  1507.  
  1508.    @@ This function should be rewritten and placed in cplus-search.c.  */
  1509. tree
  1510. build_offset_ref (cname, name)
  1511.      tree cname, name;
  1512. {
  1513.   tree decl, type, fnfields, fields, t = error_mark_node;
  1514.   tree basetypes = NULL_TREE;
  1515.   int dont_use_this = 0;
  1516.   int dtor = 0;
  1517.  
  1518.   if (TREE_CODE (cname) == SCOPE_REF)
  1519.     {
  1520.       sorry ("multiple scope qualifications in build_offset_ref");
  1521.       return error_mark_node;
  1522.     }
  1523.  
  1524.   if (! is_aggr_typedef (cname, 1))
  1525.     return error_mark_node;
  1526.  
  1527.   type = TREE_TYPE (TREE_TYPE (cname));
  1528.  
  1529.   if (TREE_CODE (name) == BIT_NOT_EXPR)
  1530.     {
  1531.       dtor = 1;
  1532.       name = TREE_OPERAND (name, 0);
  1533.     }
  1534.  
  1535.   if (TYPE_SIZE (type) == 0)
  1536.     {
  1537.       t = IDENTIFIER_CLASS_VALUE (name);
  1538.       if (t == 0)
  1539.     {
  1540.       error_with_aggr_type (type, "incomplete type `%s' does not have member `%s'", IDENTIFIER_POINTER (name));
  1541.       return error_mark_node;
  1542.     }
  1543.       if (TREE_CODE (t) == TYPE_DECL)
  1544.     {
  1545.       error_with_decl (t, "member `%s' is just a type declaration");
  1546.       return error_mark_node;
  1547.     }
  1548.       if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
  1549.     {
  1550.       TREE_USED (t) = 1;
  1551.       return t;
  1552.     }
  1553.       if (TREE_CODE (t) == FIELD_DECL)
  1554.     sorry ("use of member in incomplete aggregate type");
  1555.       else if (TREE_CODE (t) == FUNCTION_DECL)
  1556.     sorry ("use of member function in incomplete aggregate type");
  1557.       else
  1558.     abort ();
  1559.       return error_mark_node;
  1560.     }
  1561.  
  1562.   /* Unresolved multi-arity operator.  */
  1563.   if (TREE_CODE (name) == OP_IDENTIFIER)
  1564.     {
  1565.       t = copy_node (name);
  1566.       TREE_TYPE (t) = unknown_type_node;
  1567.       return t;
  1568.     }
  1569.   if (TREE_CODE (name) == TYPE_EXPR)
  1570.     /* Pass a TYPE_DECL to build_component_type_expr.  */
  1571.     return build_component_type_expr (TREE_TYPE (cname), name, NULL_TREE, 1);
  1572.  
  1573.   fnfields = lookup_fnfields (CLASSTYPE_AS_LIST (type), name, 0);
  1574.   fields = lookup_field (type, name, 0);
  1575.  
  1576.   if (fields == error_mark_node)
  1577.     return error_mark_node;
  1578.  
  1579.   if (fnfields)
  1580.     {
  1581.       basetypes = TREE_PURPOSE (fnfields);
  1582.  
  1583.       /* Go from the TREE_BASELINK to the member function info.  */
  1584.       t = TREE_VALUE (fnfields);
  1585.  
  1586.       if (fields)
  1587.     {
  1588.       if (DECL_FIELD_CONTEXT (fields) == DECL_FIELD_CONTEXT (t))
  1589.         {
  1590.           error ("ambiguous member reference: member `%s' defined as both field and function",
  1591.              IDENTIFIER_POINTER (name));
  1592.           return error_mark_node;
  1593.         }
  1594.       if (get_base_type (DECL_FIELD_CONTEXT (fields), DECL_FIELD_CONTEXT (t), 0))
  1595.         ;
  1596.       else if (get_base_type (DECL_FIELD_CONTEXT (t), DECL_FIELD_CONTEXT (fields), 0))
  1597.         t = fields;
  1598.       else
  1599.         {
  1600.           error ("ambiguous member reference: member `%s' derives from distinct classes in multiple inheritance lattice");
  1601.           return error_mark_node;
  1602.         }
  1603.     }
  1604.  
  1605.       if (t == TREE_VALUE (fnfields))
  1606.     {
  1607.       extern int flag_save_memoized_contexts;
  1608.  
  1609.       /* This does not handle visibility checking yet.  */
  1610.       if (TREE_CHAIN (t) == NULL_TREE || dtor)
  1611.         {
  1612.           enum visibility_type visibility;
  1613.  
  1614.           /* unique functions are handled easily.  */
  1615.         unique:
  1616.           visibility = compute_visibility (basetypes, t);
  1617.           if (visibility == visibility_protected)
  1618.         {
  1619.           error_with_decl (t, "member function `%s' is protected");
  1620.           error ("in this context");
  1621.           return error_mark_node;
  1622.         }
  1623.           if (visibility == visibility_private)
  1624.         {
  1625.           error_with_decl (t, "member function `%s' is private");
  1626.           error ("in this context");
  1627.           return error_mark_node;
  1628.         }
  1629.           return build (OFFSET_REF, TREE_TYPE (t), NULL_TREE, t);
  1630.         }
  1631.  
  1632.       /* overloaded functions may need more work.  */
  1633.       if (cname == name)
  1634.         {
  1635.           if (TYPE_HAS_DESTRUCTOR (type)
  1636.           && TREE_CHAIN (TREE_CHAIN (t)) == NULL_TREE)
  1637.         {
  1638.           t = TREE_CHAIN (t);
  1639.           goto unique;
  1640.         }
  1641.         }
  1642.       if (flag_save_memoized_contexts
  1643.           && !TREE_PERMANENT (fnfields)
  1644.           && global_bindings_p ())
  1645.         fnfields = copy_list (fnfields);
  1646.       t = build_tree_list (error_mark_node, fnfields);
  1647.       TREE_TYPE (t) = build_member_type (type, unknown_type_node);
  1648.       return t;
  1649.     }
  1650.     }
  1651.  
  1652.   /* Now that we know we are looking for a field, see if we
  1653.      have access to that field.  Lookup_field will give us the
  1654.      error message.  */
  1655.  
  1656.   if (current_class_type == 0
  1657.       || get_base_distance (type, current_class_type, 0, &basetypes) == -1)
  1658.     dont_use_this = 1;
  1659.  
  1660.   if (dont_use_this)
  1661.     {
  1662.       basetypes = CLASSTYPE_AS_LIST (type);
  1663.       decl = build1 (NOP_EXPR,
  1664.              TREE_TYPE (TREE_TYPE (cname)),
  1665.              error_mark_node);
  1666.     }
  1667.   else if (current_class_decl == 0)
  1668.     decl = build1 (NOP_EXPR,
  1669.            TREE_TYPE (TREE_TYPE (cname)),
  1670.            error_mark_node);
  1671.   else decl = C_C_D;
  1672.  
  1673.   t = lookup_field (basetypes, name, 1);
  1674.  
  1675.   if (t == error_mark_node)
  1676.     return error_mark_node;
  1677.  
  1678.   if (t == NULL_TREE)
  1679.     {
  1680.       if (OPERATOR_TYPENAME_P (name))
  1681.     error ("type conversion operator not a member of type `%s'",
  1682.            IDENTIFIER_POINTER (cname));
  1683.       else
  1684.     error ("field `%s' is not a member of type `%s'",
  1685.            IDENTIFIER_POINTER (name),
  1686.            IDENTIFIER_POINTER (cname));
  1687.       return error_mark_node;
  1688.     }
  1689.  
  1690.   if (TREE_CODE (t) == TYPE_DECL)
  1691.     {
  1692.       error_with_decl (t, "member `%s' is just a type declaration");
  1693.       return error_mark_node;
  1694.     }
  1695.   /* static class members and class-specific enum
  1696.      values can be returned without further ado.  */
  1697.   if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
  1698.     {
  1699.       TREE_USED (t) = 1;
  1700.       return t;
  1701.     }
  1702.  
  1703.   /* static class functions too.  */
  1704.   if (TREE_CODE (t) == FUNCTION_DECL && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
  1705.     abort ();
  1706.  
  1707.   /* In member functions, the form `cname::name' is no longer
  1708.      equivalent to `this->cname::name'.  */
  1709.   return build (OFFSET_REF, build_member_type (type, TREE_TYPE (t)), decl, t);
  1710. }
  1711.  
  1712. /* Given an object EXP and a member function reference MEMBER,
  1713.    return the address of the actual member function.  */
  1714. tree
  1715. get_member_function (exp_addr_ptr, exp, member)
  1716.      tree *exp_addr_ptr;
  1717.      tree exp, member;
  1718. {
  1719.   tree ctype = TREE_TYPE (exp);
  1720.   tree function = save_expr (build_unary_op (ADDR_EXPR, member, 0));
  1721.  
  1722.   if (TYPE_VIRTUAL_P (ctype)
  1723.       || (flag_all_virtual == 1
  1724.       && (TYPE_OVERLOADS_METHOD_CALL_EXPR (ctype)
  1725.           || TYPE_NEEDS_WRAPPER (ctype))))
  1726.     {
  1727.       tree e0, e1, e3;
  1728.       tree exp_addr;
  1729.  
  1730.       /* Save away the unadulterated `this' pointer.  */
  1731.       exp_addr = save_expr (*exp_addr_ptr);
  1732.  
  1733.       /* Cast function to signed integer.  */
  1734.       e0 = build1 (NOP_EXPR, integer_type_node, function);
  1735.  
  1736. #ifdef VTABLE_USES_MASK
  1737.       /* If we are willing to limit the number of
  1738.      virtual functions a class may have to some
  1739.      *small* number, then if, for a function address,
  1740.      we are passed some small number, we know that
  1741.      it is a virtual function index, and work from there.  */
  1742.       e1 = build (BIT_AND_EXPR, integer_type_node, e0, vtbl_mask);
  1743. #else
  1744.       /* There is a hack here that takes advantage of
  1745.      twos complement arithmetic, and the fact that
  1746.      there are more than one UNITS to the WORD.
  1747.      If the high bit is set for the `function',
  1748.      then we pretend it is a virtual function,
  1749.      and the array indexing will knock this bit
  1750.      out the top, leaving a valid index.  */
  1751. #if UNITS_PER_WORD <= 1
  1752.       virtual_functions_lose !;
  1753. #endif
  1754.       e1 = build (GT_EXPR, integer_type_node, e0, integer_zero_node);
  1755.       e1 = build_compound_expr (tree_cons (NULL_TREE, exp_addr,
  1756.                        build_tree_list (NULL_TREE, e1)));
  1757.       e1 = save_expr (e1);
  1758. #endif
  1759.  
  1760.       if (TREE_VOLATILE (*exp_addr_ptr))
  1761.     {
  1762.       exp = build_indirect_ref (exp_addr, 0);
  1763.       *exp_addr_ptr = exp_addr;
  1764.     }
  1765.  
  1766.       /* This is really hairy: if the function pointer is a pointer
  1767.      to a non-virtual member function, then we can't go mucking
  1768.      with the `this' pointer (any more than we aleady have to
  1769.      this point).  If it is a pointer to a virtual member function,
  1770.      then we have to adjust the `this' pointer according to
  1771.      what the virtual function table tells us.  */
  1772.  
  1773.       e3 = build_vfn_ref (exp_addr_ptr, exp, e0);
  1774.       assert (e3 != error_mark_node);
  1775.  
  1776.       /* Change this pointer type from `void *' to the
  1777.      type it is really supposed to be.  */
  1778.       TREE_TYPE (e3) = TREE_TYPE (function);
  1779.  
  1780.       /* If non-virtual, use what we had originally.  Otherwise,
  1781.      use the value we get from the virtual function table.  */
  1782.       *exp_addr_ptr = build_conditional_expr (e1, exp_addr, *exp_addr_ptr);
  1783.  
  1784.       function = build_conditional_expr (e1, function, e3);
  1785.     }
  1786.   return build_indirect_ref (function, 0);
  1787. }
  1788.  
  1789. /* If a OFFSET_REF made it through to here, then it did
  1790.    not have its address taken.  */
  1791.  
  1792. tree
  1793. resolve_offset_ref (exp)
  1794.      tree exp;
  1795. {
  1796.   tree base;
  1797.   tree member;
  1798.   tree basetype, addr;
  1799.  
  1800.   if (TREE_CODE (exp) == TREE_LIST)
  1801.     return build_unary_op (ADDR_EXPR, exp, 0);
  1802.  
  1803.   assert (TREE_CODE (exp) == OFFSET_REF);
  1804.   member = TREE_OPERAND (exp, 1);
  1805.   if (TREE_STATIC (member))
  1806.     {
  1807.       /* These were static members.  */
  1808.       if (mark_addressable (member) == 0)
  1809.     return error_mark_node;
  1810.       return member;
  1811.     }
  1812.  
  1813.   /* Syntax error can cause a member which should
  1814.      have been seen as static to be grok'd as non-static.  */
  1815.   if (TREE_CODE (member) == FIELD_DECL && C_C_D == NULL_TREE)
  1816.     {
  1817.       if (TREE_ADDRESSABLE (member) == 0)
  1818.     {
  1819.       error_with_decl (member, "member `%s' is non-static in static member function context");
  1820.       TREE_ADDRESSABLE (member) = 1;
  1821.     }
  1822.       return error_mark_node;
  1823.     }
  1824.  
  1825.   base = TREE_OPERAND (exp, 0);
  1826.   assert (base != NULL_TREE);
  1827.  
  1828.   /* The first case is really just a reference to a member of `this'.  */
  1829.   if (TREE_CODE (member) == FIELD_DECL
  1830.       && (base == C_C_D
  1831.       || (TREE_CODE (base) == NOP_EXPR
  1832.           && TREE_OPERAND (base, 0) == error_mark_node)))
  1833.     {
  1834.       tree basetype_path;
  1835.       enum visibility_type visibility;
  1836.  
  1837.       basetype = DECL_CONTEXT (member);
  1838.       addr = convert_pointer_to (basetype, current_class_decl);
  1839.       get_base_distance (basetype, current_class_type, 0, &basetype_path);
  1840.       visibility = compute_visibility (basetype_path, member);
  1841.       if (visibility == visibility_public)
  1842.     return build (COMPONENT_REF, TREE_TYPE (member),
  1843.               build_indirect_ref (addr, 0), member);
  1844.       if (visibility == visibility_protected)
  1845.     {
  1846.       error_with_decl ("member `%s' is protected");
  1847.       error ("in this context");
  1848.       return error_mark_node;
  1849.     }
  1850.       if (visibility == visibility_private)
  1851.     {
  1852.       error_with_decl ("member `%s' is private");
  1853.       error ("in this context");
  1854.       return error_mark_node;
  1855.     }
  1856.       abort ();
  1857.     }
  1858.   /* If this is a reference to a member function, then return
  1859.      the address of the member function (which may involve going
  1860.      through the object's vtable), otherwise, return an expression
  1861.      for the derefernced pointer-to-member construct.  */
  1862.   addr = build_unary_op (ADDR_EXPR, base, 0);
  1863.   if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
  1864.     {
  1865.       basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (member));
  1866.       addr = convert_pointer_to (basetype, addr);
  1867.       return build_unary_op (ADDR_EXPR, get_member_function (&addr, build_indirect_ref (addr, 0), member), 0);
  1868.     }
  1869.   else if (TREE_CODE (TREE_TYPE (member)) == OFFSET_TYPE)
  1870.     {
  1871.       basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (member));
  1872.       addr = convert_pointer_to (basetype, addr);
  1873.       member = convert (ptr_type_node, build_unary_op (ADDR_EXPR, member, 0));
  1874.       return build1 (INDIRECT_REF, TREE_TYPE (exp),
  1875.              build (PLUS_EXPR, ptr_type_node, addr, member));
  1876.     }
  1877.   abort ();
  1878. }
  1879.  
  1880. /* Return either DECL or its known constant value (if it has one).  */
  1881.  
  1882. tree
  1883. decl_constant_value (decl)
  1884.      tree decl;
  1885. {
  1886.   if (
  1887. #if 0
  1888.       /* These may be necessary for C, but they break C++.  */
  1889.       ! TREE_PUBLIC (decl)
  1890.       /* Don't change a variable array bound or initial value to a constant
  1891.      in a place where a variable is invalid.  */
  1892.       && current_function_decl != 0
  1893.       && ! pedantic
  1894.       &&
  1895. #endif /* 0 */
  1896.       ! TREE_THIS_VOLATILE (decl)
  1897.       && DECL_INITIAL (decl) != 0
  1898.       && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
  1899.       /* This is invalid if initial value is not constant.
  1900.      If it has either a function call, a memory reference,
  1901.      or a variable, then re-evaluating it could give different results.  */
  1902.       && TREE_LITERAL (DECL_INITIAL (decl))
  1903. #if 0
  1904.       /* Check for cases where this is sub-optimal, even though valid.  */
  1905.       && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
  1906.       && DECL_MODE (decl) != BLKmode
  1907. #endif
  1908.       )
  1909.     return DECL_INITIAL (decl);
  1910.   return decl;
  1911. }
  1912.  
  1913. /* Friend handling routines.  */
  1914. /* Friend data structures:
  1915.  
  1916.    Friend lists come from TYPE_DECL nodes.  Since all aggregate
  1917.    types are automatically typedef'd, these node are guaranteed
  1918.    to exist.
  1919.  
  1920.    The TREE_PURPOSE of a friend list is the name of the friend,
  1921.    and its TREE_VALUE is another list.
  1922.  
  1923.    The TREE_PURPOSE of that list is a type, which allows
  1924.    all functions of a given type to be friends.
  1925.    The TREE_VALUE of that list is an individual function
  1926.    which is a friend.
  1927.  
  1928.    Non-member friends will match only by their DECL.  Their
  1929.    member type is NULL_TREE, while the type of the inner
  1930.    list will either be of aggregate type or error_mark_node.  */
  1931.  
  1932. /* Tell if this function specified by FUNCTION_DECL
  1933.    can be a friend of type TYPE.
  1934.    Return nonzero if friend, zero otherwise.
  1935.  
  1936.    DECL can be zero if we are calling a constructor or accessing a
  1937.    member in global scope.  */
  1938. int
  1939. is_friend (type, decl)
  1940.      tree type, decl;
  1941. {
  1942.   tree typedecl = TYPE_NAME (type);
  1943.   tree ctype = NULL_TREE;
  1944.   tree list;
  1945.   tree name;
  1946.  
  1947.   if (decl == NULL_TREE)
  1948.     return 0;
  1949.  
  1950.   if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
  1951.     ctype = TYPE_METHOD_BASETYPE (TREE_TYPE (decl));
  1952.   else if (DECL_STATIC_FUNCTION_P (decl))
  1953.     ctype = DECL_CONTEXT (decl);
  1954.   if (ctype)
  1955.     {
  1956.       list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (typedecl));
  1957.       while (list)
  1958.     {
  1959.       if (ctype == TREE_VALUE (list))
  1960.         return 1;
  1961.       list = TREE_CHAIN (list);
  1962.     }
  1963.     }
  1964.  
  1965.   list = DECL_FRIENDLIST (typedecl);
  1966.   name = DECL_ORIGINAL_NAME (decl);
  1967.   while (list)
  1968.     {
  1969.       if (name == TREE_PURPOSE (list))
  1970.     {
  1971.       tree friends = TREE_VALUE (list);
  1972.       name = DECL_NAME (decl);
  1973.       while (friends)
  1974.         {
  1975.           if (ctype == TREE_PURPOSE (friends))
  1976.         return 1;
  1977.           if (name == DECL_NAME (TREE_VALUE (friends)))
  1978.         return 1;
  1979.           friends = TREE_CHAIN (friends);
  1980.         }
  1981.       return 0;
  1982.     }
  1983.       list = TREE_CHAIN (list);
  1984.     }
  1985.   return 0;
  1986. }
  1987.  
  1988. /* Add a new friend to the friends of the aggregate type TYPE.
  1989.    DECL is the FUNCTION_DECL of the friend being added.  */
  1990. static void
  1991. add_friend (type, decl)
  1992.      tree type, decl;
  1993. {
  1994.   tree typedecl = TYPE_NAME (type);
  1995.   tree list = DECL_FRIENDLIST (typedecl);
  1996.   tree name = DECL_ORIGINAL_NAME (decl);
  1997.   tree ctype = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
  1998.     ? TYPE_METHOD_BASETYPE (TREE_TYPE (decl)) : error_mark_node;
  1999.  
  2000.   while (list)
  2001.     {
  2002.       if (name == TREE_PURPOSE (list))
  2003.     {
  2004.       tree friends = TREE_VALUE (list);
  2005.       while (friends)
  2006.         {
  2007.           if (decl == TREE_VALUE (friends))
  2008.         {
  2009.           warning_with_decl (decl, "`%s' is already a friend of class `%s'", IDENTIFIER_POINTER (DECL_NAME (typedecl)));
  2010.           return;
  2011.         }
  2012.           friends = TREE_CHAIN (friends);
  2013.         }
  2014.       TREE_VALUE (list) = tree_cons (ctype, decl, TREE_VALUE (list));
  2015.       return;
  2016.     }
  2017.       list = TREE_CHAIN (list);
  2018.     }
  2019.   DECL_FRIENDLIST (typedecl)
  2020.     = tree_cons (DECL_ORIGINAL_NAME (decl),
  2021.          build_tree_list (error_mark_node, decl),
  2022.          DECL_FRIENDLIST (typedecl));
  2023.   if (! strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
  2024.          OPERATOR_MODIFY_FORMAT,
  2025.          OPERATOR_MODIFY_LENGTH))
  2026.     {
  2027.       tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
  2028.       TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
  2029.       TYPE_GETS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
  2030.       if (parmtypes && TREE_CHAIN (parmtypes))
  2031.     {
  2032.       tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
  2033.       if (TREE_CODE (parmtype) == REFERENCE_TYPE
  2034.           && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
  2035.         {
  2036.           TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
  2037.           TYPE_GETS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
  2038.         }
  2039.     }
  2040.     }
  2041. }
  2042.  
  2043. /* Declare that every member function NAME in FRIEND_TYPE
  2044.    (which may be NULL_TREE) is a friend of type TYPE.  */
  2045. static void
  2046. add_friends (type, name, friend_type)
  2047.      tree type, name, friend_type;
  2048. {
  2049.   tree typedecl = TYPE_NAME (type);
  2050.   tree list = DECL_FRIENDLIST (typedecl);
  2051.  
  2052.   while (list)
  2053.     {
  2054.       if (name == TREE_PURPOSE (list))
  2055.     {
  2056.       tree friends = TREE_VALUE (list);
  2057.       while (friends && TREE_PURPOSE (friends) != friend_type)
  2058.         friends = TREE_CHAIN (friends);
  2059.       if (friends)
  2060.         if (friend_type)
  2061.           warning ("method `%s::%s' is already a friend of class",
  2062.                TYPE_NAME_STRING (friend_type),
  2063.                IDENTIFIER_POINTER (name));
  2064.         else
  2065.           warning ("function `%s' is already a friend of class `%s'",
  2066.                IDENTIFIER_POINTER (name),
  2067.                IDENTIFIER_POINTER (DECL_NAME (typedecl)));
  2068.       else
  2069.         TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
  2070.                        TREE_VALUE (list));
  2071.       return;
  2072.     }
  2073.       list = TREE_CHAIN (list);
  2074.     }
  2075.   DECL_FRIENDLIST (typedecl) =
  2076.     tree_cons (name,
  2077.            build_tree_list (friend_type, NULL_TREE),
  2078.            DECL_FRIENDLIST (typedecl));
  2079.   if (! strncmp (name, OPERATOR_MODIFY_FORMAT, OPERATOR_MODIFY_LENGTH))
  2080.     {
  2081.       TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
  2082.       TYPE_GETS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
  2083.       sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
  2084.     }
  2085. }
  2086.  
  2087. /* Set up a cross reference so that type TYPE will
  2088.    make member function CTYPE::DECL a friend when CTYPE
  2089.    is finally defined.  */
  2090. void
  2091. xref_friend (type, decl, ctype)
  2092.      tree type, decl, ctype;
  2093. {
  2094.   tree typedecl = TYPE_NAME (type);
  2095.   tree friend_decl = TYPE_NAME (ctype);
  2096.   tree t = tree_cons (NULL_TREE, ctype, DECL_UNDEFINED_FRIENDS (typedecl));
  2097.  
  2098.   DECL_UNDEFINED_FRIENDS (typedecl) = t;
  2099.   SET_DECL_WAITING_FRIENDS (friend_decl, tree_cons (type, t, DECL_WAITING_FRIENDS (friend_decl)));
  2100.   TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = decl;
  2101. }
  2102.  
  2103. /* Set up a cross reference so that functions with name NAME and
  2104.    type CTYPE know that they are friends of TYPE.  */
  2105. void
  2106. xref_friends (type, name, ctype)
  2107.      tree type, name, ctype;
  2108. {
  2109.   tree typedecl = TYPE_NAME (type);
  2110.   tree friend_decl = TYPE_NAME (ctype);
  2111.   tree t = tree_cons (NULL_TREE, ctype,
  2112.               DECL_UNDEFINED_FRIENDS (typedecl));
  2113.  
  2114.   DECL_UNDEFINED_FRIENDS (typedecl) = t;
  2115.   SET_DECL_WAITING_FRIENDS (friend_decl, tree_cons (type, t, DECL_WAITING_FRIENDS (friend_decl)));
  2116.   TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = name;
  2117. }
  2118.  
  2119. /* Make FRIEND_TYPE a friend class to TYPE.  If FRIEND_TYPE has already
  2120.    been defined, we make all of its member functions friends of
  2121.    TYPE.  If not, we make it a pending friend, which can later be added
  2122.    when its definition is seen.  If a type is defined, then its TYPE_DECL's
  2123.    DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
  2124.    classes that are not defined.  If a type has not yet been defined,
  2125.    then the DECL_WAITING_FRIENDS contains a list of types
  2126.    waiting to make it their friend.  Note that these two can both
  2127.    be in use at the same time!  */
  2128. void
  2129. make_friend_class (type, friend_type)
  2130.      tree type, friend_type;
  2131. {
  2132.   tree classes;
  2133.  
  2134.   if (type == friend_type)
  2135.     {
  2136.       warning ("class `%s' is implicitly friends with itself",
  2137.            TYPE_NAME_STRING (type));
  2138.       return;
  2139.     }
  2140.  
  2141.   classes = CLASSTYPE_FRIEND_CLASSES (type);
  2142.   while (classes && TREE_VALUE (classes) != friend_type)
  2143.     classes = TREE_CHAIN (classes);
  2144.   if (classes)
  2145.     warning ("class `%s' is already friends with class `%s'",
  2146.          TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
  2147.   else
  2148.     {
  2149.       CLASSTYPE_FRIEND_CLASSES (type)
  2150.     = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
  2151.     }
  2152. }
  2153.  
  2154. /* Main friend processor.  This is large, and for modularity purposes,
  2155.    has been removed from grokdeclarator.  It returns `void_type_node'
  2156.    to indicate that something happened, though a FIELD_DECL is
  2157.    not returned.
  2158.  
  2159.    CTYPE is the class this friend belongs to.
  2160.  
  2161.    DECLARATOR is the name of the friend.
  2162.  
  2163.    DECL is the FUNCTION_DECL that the friend is.
  2164.  
  2165.    In case we are parsing a friend which is part of an inline
  2166.    definition, we will need to store PARM_DECL chain that comes
  2167.    with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
  2168.  
  2169.    FLAGS is just used for `grokclassfn'.
  2170.  
  2171.    QUALS say what special qualifies should apply to the object
  2172.    pointed to by `this'.  */
  2173. tree
  2174. do_friend (ctype, declarator, decl, parmdecls, flags, quals)
  2175.      tree ctype, declarator, decl, parmdecls;
  2176.      enum overload_flags flags;
  2177.      tree quals;
  2178. {
  2179.   if (ctype)
  2180.     {
  2181.       tree cname = TYPE_NAME (ctype);
  2182.       if (TREE_CODE (cname) == TYPE_DECL)
  2183.     cname = DECL_NAME (cname);
  2184.  
  2185.       /* A method friend.  */
  2186.       if (TREE_CODE (decl) == FUNCTION_DECL)
  2187.     {
  2188.       if (flags == NO_SPECIAL && ctype && declarator == cname)
  2189.         DECL_CONSTRUCTOR_P (decl) = 1;
  2190.  
  2191.       /* This will set up DECL_ARGUMENTS for us.  */
  2192.       grokclassfn (ctype, cname, decl, flags, TYPE_SIZE (ctype) != 0, quals);
  2193.       if (TREE_TYPE (decl) != error_mark_node)
  2194.         {
  2195.           if (TYPE_SIZE (ctype))
  2196.         {
  2197.           /* We don't call pushdecl here yet, or ever on this
  2198.              actual FUNCTION_DECL.  We must preserve its TREE_CHAIN
  2199.              until the end.  */
  2200.           make_decl_rtl (decl, NULL_TREE, 1);
  2201.           add_friend (current_class_type, decl);
  2202.         }
  2203.           else
  2204.         xref_friend (current_class_type, decl, ctype);
  2205.           DECL_FRIEND_P (decl) = 1;
  2206.         }
  2207.     }
  2208.       else
  2209.     {
  2210.       /* Possibly a bunch of method friends.  */
  2211.  
  2212.       /* Get the class they belong to.  */
  2213.       tree ctype = TREE_TYPE (TREE_TYPE (cname));
  2214.  
  2215.       /* This class is defined, use its methods now.  */
  2216.       if (TYPE_SIZE (ctype))
  2217.         {
  2218.           tree fields = lookup_fnfields (CLASSTYPE_AS_LIST (ctype), declarator, 0);
  2219.           if (fields)
  2220.         add_friends (current_class_type, declarator, ctype);
  2221.           else
  2222.         error ("method `%s' is not a member of class `%s'",
  2223.                IDENTIFIER_POINTER (declarator),
  2224.                IDENTIFIER_POINTER (cname));
  2225.         }
  2226.       else
  2227.         xref_friends (current_class_type, declarator, ctype);
  2228.       decl = void_type_node;
  2229.     }
  2230.     }
  2231.   else if (TREE_CODE (decl) == FUNCTION_DECL
  2232.        && ((IDENTIFIER_LENGTH (declarator) == 4
  2233.         && IDENTIFIER_POINTER (declarator)[0] == 'm'
  2234.         && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
  2235.            || (IDENTIFIER_LENGTH (declarator) > 10
  2236.            && IDENTIFIER_POINTER (declarator)[0] == '_'
  2237.            && IDENTIFIER_POINTER (declarator)[1] == '_'
  2238.            && strncmp (IDENTIFIER_POINTER (declarator)+2,
  2239.                    "builtin_", 8) == 0)))
  2240.     {
  2241.       /* raw "main", and builtin functions never gets overloaded,
  2242.      but they can become friends.  */
  2243.       TREE_PUBLIC (decl) = 1;
  2244.       add_friend (current_class_type, decl);
  2245.       DECL_FRIEND_P (decl) = 1;
  2246.       if (IDENTIFIER_POINTER (declarator)[0] == '_')
  2247.     {
  2248.       if (! strcmp (IDENTIFIER_POINTER (declarator)+10, "new"))
  2249.         TREE_GETS_NEW (current_class_type) = 0;
  2250.       else if (! strcmp (IDENTIFIER_POINTER (declarator)+10, "delete"))
  2251.         TREE_GETS_DELETE (current_class_type) = 0;
  2252.     }
  2253.       decl = void_type_node;
  2254.     }
  2255.   /* A global friend.
  2256.      @@ or possibly a friend from a base class ?!?  */
  2257.   else if (TREE_CODE (decl) == FUNCTION_DECL)
  2258.     {
  2259.       /* Friends must all go through the overload machinery,
  2260.      even though they may not technically be overloaded.
  2261.  
  2262.      Note that because classes all wind up being top-level
  2263.      in their scope, their friend wind up in top-level scope as well.  */
  2264.       DECL_NAME (decl) =
  2265.     build_decl_overload (IDENTIFIER_POINTER (declarator),
  2266.                  TYPE_ARG_TYPES (TREE_TYPE (decl)),
  2267.                  TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
  2268.       DECL_ASSEMBLER_NAME (decl) = IDENTIFIER_POINTER (DECL_NAME (decl));
  2269.       DECL_ARGUMENTS (decl) = parmdecls;
  2270.       /* We can call pushdecl here, because the TREE_CHAIN of this
  2271.      FUNCTION_DECL is not needed for other purposes.  */
  2272.       decl = pushdecl_top_level (decl);
  2273.  
  2274.       make_decl_rtl (decl, NULL_TREE, 1);
  2275.       add_friend (current_class_type, decl);
  2276.  
  2277.       if (! TREE_OVERLOADED (declarator)
  2278.       && IDENTIFIER_GLOBAL_VALUE (declarator)
  2279.       && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (declarator)) == FUNCTION_DECL)
  2280.     {
  2281.       error ("friend `%s' implicitly overloaded",
  2282.          IDENTIFIER_POINTER (declarator));
  2283.       error_with_decl (IDENTIFIER_GLOBAL_VALUE (declarator),
  2284.                "after declaration of non-overloaded `%s'");
  2285.     }
  2286.       DECL_FRIEND_P (decl) = 1;
  2287.       DECL_OVERLOADED (decl) = 1;
  2288.       TREE_OVERLOADED (declarator) = 1;
  2289.       push_overloaded_decl (decl);
  2290.     }
  2291.   else
  2292.     {
  2293.       /* @@ Should be able to ingest later definitions of this function
  2294.      before use.  */
  2295.       tree decl = IDENTIFIER_GLOBAL_VALUE (declarator);
  2296.       if (decl == NULL_TREE)
  2297.     {
  2298.       warning ("implicitly declaring `%s' as struct",
  2299.            IDENTIFIER_POINTER (declarator));
  2300.       decl = xref_tag (record_type_node, declarator, NULL_TREE);
  2301.       decl = TYPE_NAME (decl);
  2302.     }
  2303.  
  2304.       /* Allow abbreviated declarations of overloaded functions,
  2305.      but not if those functions are really class names.  */
  2306.       if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
  2307.     {
  2308.       warning ("`friend %s' archaic, use `friend class %s' instead",
  2309.            IDENTIFIER_POINTER (declarator),
  2310.            IDENTIFIER_POINTER (declarator));
  2311.       decl = TREE_TYPE (TREE_PURPOSE (decl));
  2312.     }
  2313.  
  2314.       if (TREE_CODE (decl) == TREE_LIST)
  2315.     add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
  2316.       else
  2317.     make_friend_class (current_class_type, TREE_TYPE (decl));
  2318.       decl = void_type_node;
  2319.     }
  2320.   return decl;
  2321. }
  2322.  
  2323. /* TYPE has now been defined.  It may, however, have a number of things
  2324.    waiting make make it their friend.  We resolve these references
  2325.    here.  */
  2326. void
  2327. embrace_waiting_friends (type)
  2328.      tree type;
  2329. {
  2330.   tree decl = TYPE_NAME (type);
  2331.   tree waiters;
  2332.  
  2333.   if (TREE_CODE (decl) != TYPE_DECL)
  2334.     return;
  2335.  
  2336.   for (waiters = DECL_WAITING_FRIENDS (decl); waiters;
  2337.        waiters = TREE_CHAIN (waiters))
  2338.     {
  2339.       tree waiter = TREE_PURPOSE (waiters);
  2340.       tree waiter_prev = TREE_VALUE (waiters);
  2341.       tree decl = TREE_TYPE (waiters);
  2342.       tree name = decl ? (TREE_CODE (decl) == IDENTIFIER_NODE
  2343.               ? decl : DECL_ORIGINAL_NAME (decl)) : NULL_TREE;
  2344.       if (name)
  2345.     {
  2346.       /* @@ There may be work to be done since we have not verified
  2347.          @@ consistency between original and friend declarations
  2348.          @@ of the functions waiting to become friends.  */
  2349.       tree field = lookup_fnfields (CLASSTYPE_AS_LIST (type), name, 0);
  2350.       if (field)
  2351.         if (decl == name)
  2352.           add_friends (waiter, name, type);
  2353.         else
  2354.           add_friend (waiter, decl);
  2355.       else
  2356.         error_with_file_and_line (DECL_SOURCE_FILE (TYPE_NAME (waiter)),
  2357.                       DECL_SOURCE_LINE (TYPE_NAME (waiter)),
  2358.                       "no method `%s' defined in class `%s' to be friend",
  2359.                       IDENTIFIER_POINTER (DECL_ORIGINAL_NAME (TREE_TYPE (waiters))),
  2360.                       TYPE_NAME_STRING (type));
  2361.     }
  2362.       else
  2363.     make_friend_class (type, waiter);
  2364.  
  2365.       if (TREE_CHAIN (waiter_prev))
  2366.     TREE_CHAIN (waiter_prev) = TREE_CHAIN (TREE_CHAIN (waiter_prev));
  2367.       else
  2368.     DECL_UNDEFINED_FRIENDS (TYPE_NAME (waiter)) = NULL_TREE;
  2369.     }
  2370. }
  2371.  
  2372. /* Generate a C++ "new" expression. DECL is either a TREE_LIST
  2373.    (which needs to go through some sort of groktypename) or it
  2374.    is the name of the class we are newing. INIT is an initialization value.
  2375.    It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
  2376.    If INIT is void_type_node, it means do *not* call a constructor
  2377.    for this instance.
  2378.  
  2379.    For types with constructors, the data returned is initialized
  2380.    by the approriate constructor.
  2381.  
  2382.    Whether the type has a constructor or not, if it has a pointer
  2383.    to a virtual function table, then that pointer is set up
  2384.    here.
  2385.  
  2386.    Unless I am mistaken, a call to new () will return initialized
  2387.    data regardless of whether the constructor itself is private or
  2388.    not.
  2389.  
  2390.    Parameter USER_PARMS, if non-null, is passed as a second parameter
  2391.    to a function called "__user_new".  This function should behave in
  2392.    a similar fashion to "__builtin_new", except that it may allocate
  2393.    storage in a more user-defined way.  */
  2394.  
  2395. tree
  2396. build_new (user_parms, decl, init)
  2397.      tree user_parms;
  2398.      tree decl, init;
  2399. {
  2400.   extern tree require_complete_type ();    /* typecheck.c */
  2401.   tree type, true_type, size, rval;
  2402.   tree init1 = NULL_TREE, nelts;
  2403.   int has_call = 0, has_array = 0;
  2404.   int use_global_new = 0;
  2405.   tree alignment = NULL_TREE;
  2406.  
  2407.   if (decl == error_mark_node)
  2408.     return error_mark_node;
  2409.  
  2410.   if (user_parms && TREE_PURPOSE (user_parms) == error_mark_node)
  2411.     {
  2412.       use_global_new = 1;
  2413.       user_parms = TREE_VALUE (user_parms);
  2414.     }
  2415.  
  2416.   if (TREE_CODE (decl) == TREE_LIST)
  2417.     {
  2418.       tree absdcl = TREE_VALUE (decl);
  2419.       tree last_absdcl = NULL_TREE;
  2420.  
  2421.       nelts = integer_one_node;
  2422.  
  2423.       if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
  2424.     {
  2425.       /* probably meant to be a call */
  2426.       has_call = 1;
  2427.       init1 = TREE_OPERAND (absdcl, 1);
  2428.       absdcl = TREE_OPERAND (absdcl, 0);
  2429.       TREE_VALUE (decl) = absdcl;
  2430.     }
  2431.       while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
  2432.     {
  2433.       last_absdcl = absdcl;
  2434.       absdcl = TREE_OPERAND (absdcl, 0);
  2435.     }
  2436.  
  2437.       if (last_absdcl)
  2438.     {
  2439.       tree save_absdecl = TREE_VALUE (decl);
  2440.       TREE_VALUE (decl) = last_absdcl;
  2441.       true_type = groktypename (decl);
  2442.       TREE_VALUE (decl) = save_absdecl;
  2443.     }
  2444.       else
  2445.     true_type = groktypename (decl);
  2446.  
  2447.       while (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
  2448.     {
  2449.       /* probably meant to be a vec new */
  2450.       tree this_nelts;
  2451.  
  2452.       has_array = 1;
  2453.       this_nelts = save_expr (TREE_OPERAND (absdcl, 1));
  2454.       absdcl = TREE_OPERAND (absdcl, 0);
  2455.       if (this_nelts == NULL_TREE)
  2456.         error ("new of array type fails to specify size");
  2457.       else if (this_nelts == integer_zero_node)
  2458.         {
  2459.           warning ("zero size array reserves no space");
  2460.           nelts = integer_zero_node;
  2461.         }
  2462.       else
  2463.         nelts = build_binary_op (MULT_EXPR, nelts, this_nelts);
  2464.     }
  2465.  
  2466.       if (last_absdcl)
  2467.     TREE_OPERAND (last_absdcl, 0) = absdcl;
  2468.       else
  2469.     TREE_VALUE (decl) = absdcl;
  2470.  
  2471.       type = groktypename (decl);
  2472.       if (! type || type == error_mark_node
  2473.       || true_type == error_mark_node)
  2474.     return error_mark_node;
  2475.  
  2476.       type = TYPE_MAIN_VARIANT (type);
  2477.       if (type == void_type_node)
  2478.     {
  2479.       error ("invalid type: `void []'");
  2480.       return error_mark_node;
  2481.     }
  2482.     }
  2483.   else if (TREE_CODE (decl) == IDENTIFIER_NODE)
  2484.     {
  2485.       if (TREE_TYPE (decl))
  2486.     {
  2487.       /* An aggregate type.  */
  2488.       decl = TREE_TYPE (decl);
  2489.       type = TREE_TYPE (decl);
  2490.     }
  2491.       else
  2492.     {
  2493.       /* A builtin type.  */
  2494.       decl = lookup_name (decl);
  2495.       assert (TREE_CODE (decl) == TYPE_DECL);
  2496.       type = TREE_TYPE (decl);
  2497.     }
  2498.       true_type = type;
  2499.     }
  2500.   else if (TREE_CODE (decl) == TYPE_DECL)
  2501.     {
  2502.       type = TREE_TYPE (decl);
  2503.       true_type = type;
  2504.     }
  2505.   else
  2506.     {
  2507.       type = decl;
  2508.       true_type = type;
  2509.       decl = TYPE_NAME (type);
  2510.     }
  2511.  
  2512.   if (TYPE_SIZE (type) == 0)
  2513.     {
  2514.       if (type == void_type_node)
  2515.     error ("invalid type for new: `void'");
  2516.       else
  2517.     incomplete_type_error (0, type);
  2518.       return error_mark_node;
  2519.     }
  2520.  
  2521.   if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_ABSTRACT_VIRTUALS (type))
  2522.     {
  2523.       abstract_virtuals_error (NULL_TREE, type);
  2524.       return error_mark_node;
  2525.     }
  2526.  
  2527.   size = size_in_bytes (type);
  2528.   if (has_array)
  2529.     size = fold (build_binary_op (MULT_EXPR, size, nelts));
  2530.  
  2531.   /* If this type has special alignment requirements, deal with them here.  */
  2532.   if (TYPE_ALIGN (type) > BITS_PER_WORD)
  2533.     {
  2534.       alignment = fold (build (MINUS_EXPR, integer_type_node,
  2535.                    c_alignof (type), integer_one_node));
  2536.       size = fold (build (PLUS_EXPR, integer_type_node, size, alignment));
  2537.     }
  2538.  
  2539.   if (has_call)
  2540.     init = init1;
  2541.  
  2542.   if (TREE_CODE (true_type) == ARRAY_TYPE)
  2543.     type = TREE_TYPE (true_type);
  2544.   else
  2545.     type = true_type;
  2546.  
  2547. #ifdef SOS
  2548.   rval = NULL_TREE;
  2549.   if (user_parms == void_type_node)
  2550.     {
  2551.       /* Simple "new dynamic" construct.  */
  2552.       if (! IS_AGGR_TYPE (type))
  2553.     {
  2554.       error ("dynamic new can only allocate objects of aggregate type");
  2555.       return error_mark_node;
  2556.     }
  2557.       else if (! is_aggr_typedef (DECL_NAME (TYPE_NAME (type)), 1))
  2558.     return error_mark_node;
  2559.       else
  2560.     rval = build_dynamic_new (type, size, NULL_TREE, init);
  2561.     }
  2562.   else if (user_parms && TREE_CODE (user_parms) == STRING_CST)
  2563.     {
  2564.       /* A "new dynamic" construct with filename argument.  */
  2565.       if (! IS_AGGR_TYPE (type))
  2566.     {
  2567.       error ("dynamic new can only allocate objects of aggregate type");
  2568.       return error_mark_node;
  2569.     }
  2570.       else if (! is_aggr_typedef (DECL_NAME (TYPE_NAME (type)), 1))
  2571.     return error_mark_node;
  2572.       else
  2573.     rval = build_dynamic_new (type, size, user_parms, init);
  2574.     }
  2575.   if (rval)
  2576.     {
  2577.       if (alignment)
  2578.     rval = build (BIT_AND_EXPR, TYPE_POINTER_TO (type),
  2579.               rval, build (BIT_NOT_EXPR, integer_type_node, alignment));
  2580.       return rval;
  2581.     }
  2582. #endif
  2583.  
  2584.   if (user_parms)
  2585.     {
  2586.       rval = build_user_new (type, size, user_parms);
  2587.     }
  2588.   else if (has_array)
  2589.     {
  2590.       rval = build (CALL_EXPR, build_pointer_type (type),
  2591.             BIN, build_tree_list (NULL_TREE, size), 0);
  2592.       if (alignment)
  2593.     rval = build (BIT_AND_EXPR, TYPE_POINTER_TO (type),
  2594.               rval, build (BIT_NOT_EXPR, integer_type_node, alignment));
  2595.       TREE_CALLS_NEW (rval) = 1;
  2596.       TREE_VOLATILE (rval) = 1;
  2597.     }
  2598.   else
  2599.     {
  2600.       if (TYPE_LANG_SPECIFIC (type)
  2601.       && (TREE_GETS_NEW (type) && !use_global_new))
  2602.     rval = build_opfncall (NEW_EXPR, LOOKUP_NORMAL, TYPE_POINTER_TO (type), size, 0);
  2603.       else if (flag_this_is_variable
  2604.            && TYPE_HAS_CONSTRUCTOR (type) && init != void_type_node)
  2605.     {
  2606.       if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
  2607.         return build_method_call (NULL_TREE, DECL_NAME (TYPE_NAME (type)), init,
  2608.                       NULL_TREE, LOOKUP_NORMAL);
  2609.       error ("constructors take parameter lists");
  2610.       return error_mark_node;
  2611.     }
  2612.       else
  2613.     {
  2614.       rval = build (CALL_EXPR, build_pointer_type (type),
  2615.             BIN, build_tree_list (NULL_TREE, size), 0);
  2616.       if (alignment)
  2617.         rval = build (BIT_AND_EXPR, TYPE_POINTER_TO (type),
  2618.               rval, build (BIT_NOT_EXPR, integer_type_node, alignment));
  2619.       TREE_CALLS_NEW (rval) = 1;
  2620.       TREE_VOLATILE (rval) = 1;
  2621.     }
  2622.     }
  2623.   if (rval == error_mark_node)
  2624.     return error_mark_node;
  2625.   rval = save_expr (rval);
  2626.   TREE_HAS_CONSTRUCTOR (rval) = 1;
  2627.  
  2628.   /* Don't call any constructors or do any initialization.  */
  2629.   if (init == void_type_node)
  2630.     return rval;
  2631.  
  2632.   if (TYPE_NEEDS_CONSTRUCTING (type))
  2633.     {
  2634.       extern tree static_aggregates;
  2635.  
  2636.       if (current_function_decl == NULL_TREE)
  2637.     {
  2638.       /* In case of static initialization, SAVE_EXPR is good enough.  */
  2639.       init = copy_to_permanent (init);
  2640.       rval = copy_to_permanent (rval);
  2641.       static_aggregates = perm_tree_cons (init, rval, static_aggregates);
  2642.     }
  2643.       else
  2644.     {
  2645.       /* Have to wrap this in RTL_EXPR for two cases:
  2646.          in base or member initialization and if we
  2647.          are a branch of a ?: operator.  Since we
  2648.          can't easily know the latter, just do it always.  */
  2649.       tree xval = make_node (RTL_EXPR);
  2650.  
  2651.       TREE_TYPE (xval) = TREE_TYPE (rval);
  2652.       do_pending_stack_adjust ();
  2653.       start_sequence ();
  2654.  
  2655.       /* As a matter of principle, `start_sequence' should do this.  */
  2656.       emit_note (0, -1);
  2657.  
  2658.       if (has_array)
  2659.         rval = expand_vec_init (decl, rval,
  2660.                     build_binary_op (MINUS_EXPR, nelts, integer_one_node),
  2661.                     init, 0);
  2662.       else
  2663.         expand_aggr_init (build_indirect_ref (rval, 0), init, 0);
  2664.  
  2665.       do_pending_stack_adjust ();
  2666.  
  2667.       TREE_VOLATILE (xval) = 1;
  2668.       RTL_EXPR_SEQUENCE (xval) = get_insns ();
  2669.       end_sequence ();
  2670.  
  2671.       if (TREE_CODE (rval) == SAVE_EXPR)
  2672.         {
  2673.           /* Errors may cause this to not get evaluated.  */
  2674.           if (SAVE_EXPR_RTL (rval) == 0)
  2675.         SAVE_EXPR_RTL (rval) = const0_rtx;
  2676.           RTL_EXPR_RTL (xval) = SAVE_EXPR_RTL (rval);
  2677.         }
  2678.       else
  2679.         {
  2680.           assert (TREE_CODE (rval) == VAR_DECL);
  2681.           RTL_EXPR_RTL (xval) = DECL_RTL (rval);
  2682.         }
  2683.       rval = xval;
  2684.     }
  2685.     }
  2686.   else if (has_call || init)
  2687.     {
  2688.       if (IS_AGGR_TYPE (type))
  2689.     error_with_aggr_type (type, "no constructor for type `%s'");
  2690.       else
  2691.     error ("no constructor for this type");
  2692.       rval = error_mark_node;
  2693.     }
  2694.  
  2695.   return rval;
  2696. }
  2697.  
  2698. static tree
  2699. build_user_new (type, size, user_parms)
  2700.      tree type;
  2701.      tree size;
  2702.      tree user_parms;
  2703. {
  2704.   tree user_new = lookup_name (USR_NEW);
  2705.   tree rval;
  2706.  
  2707.   if (user_new == NULL_TREE)
  2708.     {
  2709.       error ("no declaration of \"__user_new\"");
  2710.       return error_mark_node;
  2711.     }
  2712.   rval = build_x_function_call (user_new, tree_cons (NULL_TREE, size, user_parms),
  2713.                 current_class_decl);
  2714.   if (rval == error_mark_node)
  2715.     return error_mark_node;
  2716.   TREE_TYPE (rval) = build_pointer_type (type);
  2717.   return rval;
  2718. }
  2719.  
  2720. #ifdef SOS
  2721. /* Build a "new dynamic" call for type TYPE.  The size
  2722.    of the object we are newing is SIZE.  If "new dynamic" was
  2723.    given with an argument, that argument is in NAME.
  2724.    PARMS contains the parameters to the constructor.
  2725.    The first parameter must be an `ImportRequest *'.
  2726.  
  2727.    This is slightly hairy, because we must find the correct
  2728.    constructor by hand.  */
  2729. static tree
  2730. build_dynamic_new (type, size, name, parms)
  2731.      tree type, size;
  2732.      tree name, parms;
  2733. {
  2734.   tree import_parms, inner_parms;
  2735.   tree import_ptr = integer_zero_node;
  2736.   /* This variable is supposed to be the address of a "struct ref"
  2737.      object, but how and where should it be defined?  */
  2738.   tree lookup_tmp = integer_zero_node;
  2739.   tree import_tmp = build_unary_op (ADDR_EXPR, get_temp_name (ptr_type_node, 0), 0);
  2740.  
  2741.   if (name)
  2742.     {
  2743.       inner_parms = tree_cons (NULL_TREE, name,
  2744.                    build_tree_list (NULL_TREE, integer_zero_node));
  2745.       inner_parms = tree_cons (NULL_TREE, lookup_tmp, inner_parms);
  2746.       inner_parms = build_function_call (__sosLookup, inner_parms);
  2747.     }
  2748.   else
  2749.     inner_parms = integer_zero_node;
  2750.  
  2751.   import_parms = build_tree_list (NULL_TREE, inner_parms);
  2752.  
  2753.   if (CLASSTYPE_DYNAMIC_FILENAME (type))
  2754.     {
  2755.       inner_parms = tree_cons (NULL_TREE, CLASSTYPE_DYNAMIC_FILENAME (type),
  2756.                    build_tree_list (NULL_TREE, integer_zero_node));
  2757.       inner_parms = tree_cons (NULL_TREE, lookup_tmp, inner_parms);
  2758.       inner_parms = build_function_call (__sosLookup, inner_parms);
  2759.     }
  2760.   else
  2761.     inner_parms = integer_zero_node;
  2762.  
  2763.   import_parms = tree_cons (NULL_TREE, inner_parms, import_parms);
  2764.   import_parms = tree_cons (NULL_TREE, CLASSTYPE_TYPENAME_AS_STRING (type), import_parms);
  2765.   /* This is one parameter which could be (but should not be) evaluated twice.  */
  2766.   TREE_VALUE (parms) = save_expr (TREE_VALUE (parms));
  2767.  
  2768.   import_parms = tree_cons (NULL_TREE, TREE_VALUE (parms), import_parms);
  2769.  
  2770.   /* SOS?? Pass the address of a temporary which can hold the pointer
  2771.      to dynamic class table, but how and where is it defined? */
  2772.   import_parms = tree_cons (NULL_TREE, import_tmp, import_parms);
  2773.  
  2774.   import_ptr = build_function_call (__sosImport, import_parms);
  2775.  
  2776.   /* SOS?? Now, generate call to ctor, but using `import_ptr' as the function
  2777.      table.  Return the result of the call to the ctor.  */
  2778.   import_ptr = build1 (NOP_EXPR, TYPE_POINTER_TO (type), import_ptr);
  2779.   return build_method_call (import_ptr, DECL_NAME (TYPE_NAME (type)),
  2780.                 tree_cons (NULL_TREE, import_tmp, parms),
  2781.                 NULL_TREE, LOOKUP_DYNAMIC);
  2782. }
  2783.  
  2784. /* Return the name of the link table (as an IDENTIFIER_NODE)
  2785.    for the given TYPE.  */
  2786. tree
  2787. get_linktable_name (type)
  2788.      tree type;
  2789. {
  2790.   char *buf = (char *)alloca (4 + TYPE_NAME_LENGTH (type) + 1);
  2791.   tree name;
  2792.  
  2793.   assert (TYPE_DYNAMIC (type));
  2794.   sprintf (buf, "ZN_%s_", TYPE_NAME_STRING (type));
  2795.   return get_identifier (buf);
  2796. }
  2797.  
  2798. /* For a given type TYPE, grovel for a function table which
  2799.    can be used to support dynamic linking.  */
  2800. tree
  2801. get_sos_dtable (type, parms)
  2802.      tree type, parms;
  2803. {
  2804.   tree classname = CLASSTYPE_TYPENAME_AS_STRING (type);
  2805.   tree filename = CLASSTYPE_DYNAMIC_FILENAME (type);
  2806.   tree dyn_vtbl;
  2807.   /* This variable is supposed to be the address of a "struct ref"
  2808.      object, but how and where should it be defined?  */
  2809.   tree lookup_tmp = integer_zero_node;
  2810.  
  2811.   assert (TYPE_DYNAMIC (type));
  2812.  
  2813.   if (filename)
  2814.     {
  2815.       tree inner_parms = tree_cons (NULL_TREE, filename,
  2816.                     build_tree_list (NULL_TREE, integer_zero_node));
  2817.       inner_parms = tree_cons (NULL_TREE, lookup_tmp, inner_parms);
  2818.       parms = build_tree_list (NULL_TREE, build_function_call (__sosLookup, inner_parms));
  2819.     }
  2820.   else
  2821.     parms = build_tree_list (NULL_TREE, integer_zero_node);
  2822.  
  2823.   parms = tree_cons (NULL_TREE, classname, parms);
  2824.  
  2825.   dyn_vtbl = build_function_call (__sosFindCode, parms);
  2826.   TREE_TYPE (dyn_vtbl) = build_pointer_type (ptr_type_node);
  2827.   return dyn_vtbl;
  2828. }
  2829. #endif
  2830.  
  2831. /* `expand_vec_init' performs initialization of a vector of aggregate
  2832.    types.
  2833.  
  2834.    DECL is passed only for error reporting, and provides line number
  2835.    and source file name information.
  2836.    BASE is the space where the vector will be.
  2837.    MAXINDEX is the maximum index of the array (one less than the
  2838.         number of elements).
  2839.    INIT is the (possibly NULL) initializer.
  2840.  
  2841.    FROM_ARRAY is 0 if we should init everything with INIT
  2842.    (i.e., every element initialized from INIT).
  2843.    FROM_ARRAY is 1 if we should index into INIT in parallel
  2844.    with initialization of DECL.
  2845.    FROM_ARRAY is 2 if we should index into INIT in parallel,
  2846.    but use assignment instead of initialization.  */
  2847.  
  2848. tree
  2849. expand_vec_init (decl, base, maxindex, init, from_array)
  2850.      tree decl, base, maxindex, init;
  2851. {
  2852.   tree rval;
  2853.   tree iterator, base2 = NULL_TREE;
  2854.   tree type = TREE_TYPE (TREE_TYPE (base));
  2855.   tree size;
  2856.  
  2857.   maxindex = convert (integer_type_node, maxindex);
  2858.   if (maxindex == error_mark_node)
  2859.     return error_mark_node;
  2860.  
  2861.   if (current_function_decl == NULL_TREE)
  2862.     {
  2863.       rval = make_tree_vec (3);
  2864.       TREE_VEC_ELT (rval, 0) = base;
  2865.       TREE_VEC_ELT (rval, 1) = maxindex;
  2866.       TREE_VEC_ELT (rval, 2) = init;
  2867.       return rval;
  2868.     }
  2869.  
  2870.   size = size_in_bytes (type);
  2871.  
  2872.   /* Set to zero in case size is <= 0.  Optimizer will delete this if
  2873.      it is not needed.  */
  2874.   rval = get_temp_regvar (TYPE_POINTER_TO (type), null_pointer_node);
  2875.   base = default_conversion (base);
  2876.   expand_assignment (rval, base, 0, 0);
  2877.   base = get_temp_regvar (TYPE_POINTER_TO (type), base);
  2878.  
  2879.   if (init != NULL_TREE
  2880.       && TREE_CODE (init) == CONSTRUCTOR
  2881.       && TREE_TYPE (init) == TREE_TYPE (decl))
  2882.     {
  2883.       /* Initialization of array from {...}.  */
  2884.       tree elts = CONSTRUCTOR_ELTS (init);
  2885.       tree baseref = build1 (INDIRECT_REF, type, base);
  2886.       tree baseinc = build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size);
  2887.       int host_i = TREE_INT_CST_LOW (maxindex);
  2888.  
  2889.       if (IS_AGGR_TYPE (type))
  2890.     {
  2891.       while (elts)
  2892.         {
  2893.           host_i -= 1;
  2894.           expand_aggr_init (baseref, TREE_VALUE (elts), 0);
  2895.  
  2896.           expand_assignment (base, baseinc, 0, 0);
  2897.           elts = TREE_CHAIN (elts);
  2898.         }
  2899.       /* Initialize any elements by default if possible.  */
  2900.       if (host_i >= 0)
  2901.         {
  2902.           if (TYPE_NEEDS_CONSTRUCTING (type) == 0)
  2903.         {
  2904.           if (obey_regdecls)
  2905.             use_variable (DECL_RTL (base));
  2906.           goto done_init;
  2907.         }
  2908.  
  2909.           iterator = get_temp_regvar (integer_type_node,
  2910.                       build_int_2 (host_i, 0));
  2911.           init = NULL_TREE;
  2912.           goto init_by_default;
  2913.         }
  2914.     }
  2915.       else
  2916.     while (elts)
  2917.       {
  2918.         expand_assignment (baseref, TREE_VALUE (elts), 0, 0);
  2919.  
  2920.         expand_assignment (base, baseinc, 0, 0);
  2921.         elts = TREE_CHAIN (elts);
  2922.       }
  2923.  
  2924.       if (obey_regdecls)
  2925.     use_variable (DECL_RTL (base));
  2926.     }
  2927.   else
  2928.     {
  2929.       iterator = get_temp_regvar (integer_type_node, maxindex);
  2930.  
  2931.     init_by_default:
  2932.  
  2933.       /* If initializing one array from another,
  2934.      initialize element by element.  */
  2935.       if (from_array)
  2936.     {
  2937.       if (decl == NULL_TREE
  2938.           || (init && TREE_TYPE (init) != TREE_TYPE (decl)))
  2939.         {
  2940.           sorry ("initialization of array from dissimilar array type");
  2941.           return error_mark_node;
  2942.         }
  2943.       if (init)
  2944.         {
  2945.           base2 = default_conversion (init);
  2946.           base2 = get_temp_regvar (TYPE_POINTER_TO (type), base2);
  2947.         }
  2948.       else if (TYPE_LANG_SPECIFIC (type)
  2949.            && TYPE_NEEDS_CONSTRUCTING (type)
  2950.            && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
  2951.         {
  2952.           error ("initializer ends prematurely");
  2953.           return;
  2954.         }
  2955.     }
  2956.  
  2957.       expand_start_cond (build (GE_EXPR, integer_type_node,
  2958.                 iterator, integer_zero_node), 0);
  2959.       expand_start_loop_continue_elsewhere (1);
  2960.  
  2961.       if (from_array)
  2962.     {
  2963.       tree to = build1 (INDIRECT_REF, type, base);
  2964.       tree from;
  2965.  
  2966.       if (base2)
  2967.         from = build1 (INDIRECT_REF, type, base2);
  2968.       else
  2969.         from = NULL_TREE;
  2970.  
  2971.       if (from_array == 2)
  2972.         expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from));
  2973.       else if (TYPE_NEEDS_CONSTRUCTING (type))
  2974.         expand_aggr_init (to, from, 0);
  2975.       else if (from)
  2976.         expand_assignment (to, from, 0, 0);
  2977.       else abort ();
  2978.     }
  2979.       else if (TREE_CODE (type) == ARRAY_TYPE)
  2980.     {
  2981.       if (init != 0)
  2982.         sorry ("cannot initialize multi-dimensional array with initializer");
  2983.       expand_vec_init (decl, build1 (NOP_EXPR, TYPE_POINTER_TO (TREE_TYPE (type)), base),
  2984.                array_type_nelts (type), 0, 0);
  2985.     }
  2986.       else
  2987.     expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0);
  2988.  
  2989.       expand_assignment (base,
  2990.              build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size),
  2991.              0, 0);
  2992.       if (base2)
  2993.     expand_assignment (base2,
  2994.                build (PLUS_EXPR, TYPE_POINTER_TO (type), base2, size), 0, 0);
  2995.       expand_loop_continue_here ();
  2996.       expand_exit_loop_if_false (build (NE_EXPR, integer_type_node,
  2997.                     build (PREDECREMENT_EXPR, integer_type_node, iterator, integer_one_node), minus_one));
  2998.  
  2999.       if (obey_regdecls)
  3000.     {
  3001.       use_variable (DECL_RTL (base));
  3002.       if (base2)
  3003.         use_variable (DECL_RTL (base2));
  3004.     }
  3005.       expand_end_loop ();
  3006.       expand_end_cond ();
  3007.       if (obey_regdecls)
  3008.     use_variable (DECL_RTL (iterator));
  3009.     }
  3010.  done_init:
  3011.  
  3012.   if (obey_regdecls)
  3013.     use_variable (DECL_RTL (rval));
  3014.   return rval;
  3015. }
  3016.  
  3017. /* Free up storage of type TYPE, at address ADDR.
  3018.    TYPE is a POINTER_TYPE.
  3019.  
  3020.    This does not call any destructors.  */
  3021. tree
  3022. build_x_delete (type, addr, use_global_delete)
  3023.      tree type, addr;
  3024.      int use_global_delete;
  3025. {
  3026.   tree rval;
  3027.  
  3028.   if (TREE_GETS_DELETE (TREE_TYPE (type)) && !use_global_delete)
  3029.     rval = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr);
  3030.   else
  3031.     {
  3032.       rval = build (CALL_EXPR, void_type_node, BID,
  3033.             build_tree_list (NULL_TREE, addr), 0);
  3034.       TREE_VOLATILE (rval) = 1;
  3035.     }
  3036.   return rval;
  3037. }
  3038.  
  3039. /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
  3040.    ADDR is an expression which yields the store to be destroyed.
  3041.    AUTO_DELETE is nonzero if a call to DELETE should be made or not.
  3042.  
  3043.    FLAGS is the logical disjunction of zero or more LOOKUP_
  3044.    flags.  See cplus-tree.h for more info.
  3045.  
  3046.    This function does not delete an object's virtual base classes.  */
  3047. tree
  3048. build_delete (type, addr, auto_delete, flags, use_global_delete)
  3049.      tree type, addr;
  3050.      tree auto_delete;
  3051.      int flags;
  3052.      int use_global_delete;
  3053. {
  3054.   tree function, parms;
  3055.   tree member;
  3056.   tree expr;
  3057.   tree ref;
  3058.   int ptr;
  3059.  
  3060.   if (addr == error_mark_node)
  3061.     return error_mark_node;
  3062.  
  3063.   /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
  3064.      set to `error_mark_node' before it gets properly cleaned up.  */
  3065.   if (type == error_mark_node)
  3066.     return error_mark_node;
  3067.  
  3068.   type = TYPE_MAIN_VARIANT (type);
  3069.  
  3070.   if (TREE_CODE (type) == POINTER_TYPE)
  3071.     {
  3072.       type = TREE_TYPE (type);
  3073.       if (TYPE_SIZE (type) == 0)
  3074.     {
  3075.       incomplete_type_error (0, type);
  3076.       return error_mark_node;
  3077.     }
  3078.       if (! IS_AGGR_TYPE (type))
  3079.     {
  3080.       expr = build (CALL_EXPR, void_type_node,
  3081.             BID, build_tree_list (NULL_TREE, addr), 0);
  3082.       TREE_VOLATILE (expr) = 1;
  3083.       return expr;
  3084.     }
  3085.       if (TREE_VOLATILE (addr))
  3086.     addr = save_expr (addr);
  3087.       ref = build_indirect_ref (addr, 0);
  3088.       ptr = 1;
  3089.     }
  3090.   else if (TREE_CODE (type) == ARRAY_TYPE)
  3091.     return build_vec_delete (addr, array_type_nelts (type), c_sizeof (TREE_TYPE (type)),
  3092.                  NULL_TREE, auto_delete, integer_zero_node);
  3093.   else
  3094.     {
  3095.       /* Don't check PROTECT here; leave that decision to the
  3096.      destructor.  If the destructor is visible, call it,
  3097.      else report error.  */
  3098.       addr = build_unary_op (ADDR_EXPR, addr, 0);
  3099.  
  3100.       if (TREE_LITERAL (addr))
  3101.     addr = convert_pointer_to (type, addr);
  3102.       else
  3103.     addr = convert_force (build_pointer_type (type), addr);
  3104.  
  3105.       if (TREE_CODE (addr) == NOP_EXPR
  3106.       && TREE_OPERAND (addr, 0) == current_class_decl)
  3107.     ref = C_C_D;
  3108.       else
  3109.     ref = build_indirect_ref (addr, 0);
  3110.       ptr = 0;
  3111.     }
  3112.  
  3113.   assert (IS_AGGR_TYPE (type));
  3114.  
  3115.   if (! TYPE_NEEDS_DESTRUCTOR (type))
  3116.     {
  3117.       if (auto_delete == integer_zero_node)
  3118.     return build1 (NOP_EXPR, void_type_node, integer_zero_node);
  3119.       else if (TREE_GETS_DELETE (type) && !use_global_delete)
  3120.     return build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr);
  3121.       parms = build_tree_list (NULL_TREE, addr);
  3122.       expr = build (CALL_EXPR, void_type_node, BID, parms, 0);
  3123.       TREE_VOLATILE (expr) = 1;
  3124.       return expr;
  3125.     }
  3126.   parms = build_tree_list (NULL_TREE, addr);
  3127.  
  3128.   /* Below, we will reverse the order in which these calls are made.
  3129.      If we have a destructor, then that destructor will take care
  3130.      of the base classes; otherwise, we must do that here.  */
  3131.   if (TYPE_HAS_DESTRUCTOR (type))
  3132.     {
  3133.       extern tree destructor_label;
  3134.       tree field = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
  3135.       tree basetypes = CLASSTYPE_AS_LIST (type);
  3136.  
  3137.       if (flags & LOOKUP_PROTECT)
  3138.     {
  3139.       enum visibility_type visibility = compute_visibility (basetypes, field);
  3140.  
  3141.       if (visibility == visibility_private)
  3142.         {
  3143.           if (flags & LOOKUP_COMPLAIN)
  3144.         error_with_aggr_type (type, "destructor for type `%s' is private in this scope");
  3145.           return error_mark_node;
  3146.         }
  3147.       else if (visibility == visibility_protected
  3148.            && (flags & LOOKUP_PROTECTED_OK) == 0)
  3149.         {
  3150.           if (flags & LOOKUP_COMPLAIN)
  3151.         error_with_aggr_type (type, "destructor for type `%s' is protected in this scope");
  3152.           return error_mark_node;
  3153.         }
  3154.     }
  3155.  
  3156.       /* Once we are in a destructor, try not going through
  3157.      the virtual function table to find the next destructor.  */
  3158.       if (DECL_VIRTUAL_P (field)
  3159.       && ! (flags & LOOKUP_NONVIRTUAL)
  3160.       && TREE_CODE (auto_delete) != PARM_DECL
  3161.       && (ptr == 1 || ! resolves_to_fixed_type_p (ref)))
  3162.     {
  3163.       /* This destructor must be called via virtual function table.  */
  3164.       field = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (DECL_VCONTEXT (field)), 0);
  3165.       expr = convert_pointer_to (DECL_CONTEXT (field), TREE_VALUE (parms));
  3166.       if (expr != TREE_VALUE (parms))
  3167.         {
  3168.           ref = build_indirect_ref (expr, 0);
  3169.           TREE_VALUE (parms) = expr;
  3170.         }
  3171.       function = build_vfn_ref (&TREE_VALUE (parms), ref, DECL_VINDEX (field));
  3172.       if (function == error_mark_node)
  3173.         return error_mark_node;
  3174.       TREE_TYPE (function) = build_pointer_type (TREE_TYPE (field));
  3175.       TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
  3176.       expr = build_function_call (function, parms);
  3177.       if (ptr)
  3178.         {
  3179.           /* Handle the case where a virtual destructor is
  3180.          being called on an item that is 0.
  3181.  
  3182.          @@ Does this really need to be done?  */
  3183.           tree ifexp = build_binary_op (NE_EXPR, addr, integer_zero_node);
  3184. #if 0
  3185.           if (TREE_CODE (ref) == VAR_DECL
  3186.           || TREE_CODE (ref) == COMPONENT_REF)
  3187.         warning ("losing in build_delete");
  3188. #endif
  3189.           expr = build (COND_EXPR, void_type_node,
  3190.                 ifexp, expr,
  3191.                 build1 (NOP_EXPR, void_type_node, integer_zero_node));
  3192.         }
  3193.     }
  3194.       else
  3195.     {
  3196.       tree ifexp;
  3197.  
  3198.       if (TREE_CODE (ref) == VAR_DECL
  3199.           || TREE_CODE (ref) == PARM_DECL
  3200.           || TREE_CODE (ref) == COMPONENT_REF
  3201.           || TREE_CODE (ref) == ARRAY_REF)
  3202.         /* These can't be 0.  */
  3203.         ifexp = integer_one_node;
  3204.       else
  3205.         /* Handle the case where a non-virtual destructor is
  3206.            being called on an item that is 0.  */
  3207.         ifexp = build_binary_op (NE_EXPR, addr, integer_zero_node);
  3208.  
  3209.       function = field;
  3210.  
  3211.       /* Used to mean that this destructor was known to be empty,
  3212.          but that's now obsolete.  */
  3213.       assert (DECL_INITIAL (function) != void_type_node);
  3214.  
  3215.       TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
  3216.       expr = build_function_call (function, parms);
  3217.  
  3218.       if (ifexp != integer_one_node)
  3219.         expr = build (COND_EXPR, void_type_node,
  3220.               ifexp, expr,
  3221.               build1 (NOP_EXPR, void_type_node, integer_zero_node));
  3222.     }
  3223.       return expr;
  3224.     }
  3225.   else
  3226.     {
  3227.       /* This can get visibilties wrong.  */
  3228.       int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (type);
  3229.       tree basetype = n_baseclasses > 0 ? CLASSTYPE_BASECLASS (type, 1) : NULL_TREE;
  3230.       tree exprstmt = NULL_TREE;
  3231.       tree parent_auto_delete = auto_delete;
  3232.       tree cond;
  3233.  
  3234.       /* If this type does not have a destructor, but does have
  3235.      operator delete, call the parent parent destructor (if any),
  3236.      but let this node do the deleting.  Otherwise, it is ok
  3237.      to let the parent destructor do the deleting.  */
  3238.       if (TREE_GETS_DELETE (type) && !use_global_delete)
  3239.     {
  3240.       parent_auto_delete = integer_zero_node;
  3241.       if (auto_delete == integer_zero_node)
  3242.         cond = NULL_TREE;
  3243.       else
  3244.         {
  3245.           expr = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr);
  3246.           if (expr == error_mark_node)
  3247.         return error_mark_node;
  3248.           if (auto_delete != integer_one_node)
  3249.         cond = build (COND_EXPR, void_type_node,
  3250.                   build (NE_EXPR, integer_type_node, auto_delete, integer_zero_node),
  3251.                   expr,
  3252.                   build1 (NOP_EXPR, void_type_node, integer_zero_node));
  3253.           else cond = expr;
  3254.         }
  3255.     }
  3256.       else if (basetype == NULL_TREE
  3257.            || (CLASSTYPE_VIA_VIRTUAL (type, 1) == 0
  3258.            && ! TYPE_NEEDS_DESTRUCTOR (basetype)))
  3259.     {
  3260.       cond = build (COND_EXPR, void_type_node,
  3261.             build (NE_EXPR, integer_type_node, auto_delete, integer_zero_node),
  3262.             build_function_call (BID, build_tree_list (NULL_TREE, addr)),
  3263.             build1 (NOP_EXPR, void_type_node, integer_zero_node));
  3264.     }
  3265.       else cond = NULL_TREE;
  3266.  
  3267.       if (cond)
  3268.     exprstmt = build_tree_list (NULL_TREE, cond);
  3269.  
  3270.       if (basetype
  3271.       && ! CLASSTYPE_VIA_VIRTUAL (type, 1)
  3272.       && TYPE_NEEDS_DESTRUCTOR (basetype))
  3273.     {
  3274.       expr = build_delete (basetype, ref, parent_auto_delete,
  3275.                    flags|LOOKUP_PROTECTED_OK, 0);
  3276.       exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
  3277.     }
  3278.  
  3279.       for (i = 2; i <= n_baseclasses; i++)
  3280.     {
  3281.       basetype = CLASSTYPE_BASECLASS (type, i);
  3282.       if (! TYPE_NEEDS_DESTRUCTOR (basetype)
  3283.           || CLASSTYPE_VIA_VIRTUAL (type, i))
  3284.         continue;
  3285.  
  3286.       assert (DECL_OFFSET (TYPE_NAME (basetype)));
  3287.       expr = build (PLUS_EXPR, TYPE_POINTER_TO (basetype),
  3288.             addr, CLASSTYPE_OFFSET (basetype));
  3289.       expr = build_delete (TYPE_POINTER_TO (basetype), expr,
  3290.                    integer_zero_node,
  3291.                    flags|LOOKUP_PROTECTED_OK, 0);
  3292.  
  3293.       exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
  3294.     }
  3295.  
  3296.       for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
  3297.     {
  3298.       if (TREE_CODE (member) != FIELD_DECL)
  3299.         continue;
  3300.       if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
  3301.         {
  3302.           tree this_member = build_component_ref (ref, DECL_NAME (member), 0, 0);
  3303.           tree this_type = TREE_TYPE (member);
  3304.           expr = build_delete (this_type, this_member, integer_zero_node,
  3305.                    flags, 0);
  3306.           exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
  3307.           while (TREE_CODE (this_type) == ARRAY_TYPE)
  3308.         this_type = TREE_TYPE (this_type);
  3309.           if (TYPE_USES_VIRTUAL_BASECLASSES (this_type))
  3310.         {
  3311.           if (TREE_CODE (TREE_TYPE (member)) == ARRAY_TYPE)
  3312.             sorry ("delete of arrays with virtual baseclasses");
  3313.           else
  3314.             {
  3315.               expr = build_vbase_delete (TREE_TYPE (member), this_member);
  3316.               exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
  3317.             }
  3318.         }
  3319.         }
  3320.     }
  3321.  
  3322.       if (exprstmt)
  3323.     return build_compound_expr (exprstmt);
  3324.       /* Virtual base classes make this function do nothing.  */
  3325.       return build1 (NOP_EXPR, void_type_node, integer_zero_node);
  3326.     }
  3327. }
  3328.  
  3329. /* For type TYPE, delete the virtual baseclass objects of DECL.  */
  3330.  
  3331. tree
  3332. build_vbase_delete (type, decl)
  3333.      tree type, decl;
  3334. {
  3335.   tree vbases = CLASSTYPE_VBASECLASSES (type);
  3336.   tree result = NULL_TREE;
  3337.   tree addr = build_unary_op (ADDR_EXPR, decl, 0);
  3338.   assert (addr != error_mark_node);
  3339.   while (vbases)
  3340.     {
  3341.       tree this_addr = convert_force (TYPE_POINTER_TO (TREE_VALUE (vbases)), addr);
  3342.       result = tree_cons (NULL_TREE,
  3343.               build_delete (TREE_TYPE (this_addr), this_addr,
  3344.                     integer_zero_node, LOOKUP_NORMAL, 0),
  3345.               result);
  3346.       vbases = TREE_CHAIN (vbases);
  3347.     }
  3348.   return build_compound_expr (nreverse (result));
  3349. }
  3350.  
  3351. /* Build a C++ vector delete expression.
  3352.    MAXINDEX is the number of elements to be deleted.
  3353.    ELT_SIZE is the nominal size of each element in the vector.
  3354.    BASE is the expression that should yield the store to be deleted.
  3355.    DTOR_DUMMY is a placeholder for a destructor.  The library function
  3356.    __builtin_vec_delete has a pointer to function in this position.
  3357.    This function expands (or synthesizes) these calls itself.
  3358.    AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
  3359.    AUTO_DELETE say whether each item in the container should be deallocated.
  3360.  
  3361.    This also calls delete for virtual baseclasses of elements of the vector.  */
  3362. tree
  3363. build_vec_delete (base, maxindex, elt_size, dtor_dummy, auto_delete_vec, auto_delete)
  3364.      tree base, maxindex, elt_size;
  3365.      tree dtor_dummy;
  3366.      tree auto_delete_vec, auto_delete;
  3367. {
  3368.   tree ptype = TREE_TYPE (base);
  3369.   tree type;
  3370.   tree rval;
  3371.   /* Temporary variables used by the loop.  */
  3372.   tree iterator, tbase;
  3373.   tree size_exp;
  3374.  
  3375.   /* This is the body of the loop that implements the deletion of a
  3376.      single element, and moves temp variables to next elements.  */
  3377.   tree body;
  3378.  
  3379.   /* This is the LOOP_STMT that governs the deletetion of the elements.  */
  3380.   tree loop;
  3381.  
  3382.   /* This is the IF_STMT that governs whether the loop is executed at all.  */
  3383.   tree if_stmt;
  3384.  
  3385.   /* This is the LET_STMT which holds the outermost iterator of the
  3386.      loop.  It is convenient to set this variable up and test it before
  3387.      executing any other code in the loop.  */
  3388.   tree block;
  3389.  
  3390.   if (TREE_CODE (ptype) == POINTER_TYPE)
  3391.     {
  3392.       if (maxindex == 0)
  3393.     {
  3394.       error ("must specify size for non array type");
  3395.       return error_mark_node;
  3396.     }
  3397.       maxindex = convert (integer_type_node, maxindex);
  3398.       if (maxindex == error_mark_node)
  3399.     return error_mark_node;
  3400.     }
  3401.   else if (TREE_CODE (ptype) == ARRAY_TYPE)
  3402.     {
  3403.       tree amaxindex = array_type_nelts (ptype);
  3404.       int multi = 0;
  3405.  
  3406.       maxindex = fold (convert (integer_type_node, maxindex));
  3407.  
  3408.       while (1)
  3409.     {
  3410.       if (maxindex == error_mark_node || integer_zerop (maxindex))
  3411.         return error_mark_node;
  3412.       ptype = TREE_TYPE (ptype);
  3413.       if (TREE_CODE (ptype) != ARRAY_TYPE)
  3414.         break;
  3415.  
  3416.       if (multi == 0)
  3417.         {
  3418.           /* Convert from 0-based to 1-based indexing.  */
  3419.           maxindex = fold (build (PLUS_EXPR, integer_type_node,
  3420.                       maxindex, integer_one_node));
  3421.           multi = 1;
  3422.         }
  3423.       amaxindex = array_type_nelts (ptype);
  3424.       maxindex = fold (build (MULT_EXPR, integer_type_node,
  3425.                   fold (build (PLUS_EXPR, integer_type_node,
  3426.                            amaxindex, integer_one_node)),
  3427.                   maxindex));
  3428.     }
  3429.  
  3430.       if (multi)
  3431.     {
  3432.       /* Convert back to 0-based indexing.  */
  3433.       maxindex = fold (build (MINUS_EXPR, integer_type_node,
  3434.                   maxindex, integer_one_node));
  3435.       ptype = TYPE_POINTER_TO (ptype);
  3436.       base = build (NOP_EXPR, ptype, build_unary_op (ADDR_EXPR, base, 0));
  3437.     }
  3438.       else
  3439.     {
  3440.       if (amaxindex != 0
  3441.           && (TREE_CODE (maxindex) == INTEGER_CST || TREE_CODE (amaxindex) == INTEGER_CST)
  3442.           && ! tree_int_cst_equal (maxindex, amaxindex))
  3443.         warning ("argument to vector delete disagrees with declared type of array");
  3444.       base = default_conversion (base);
  3445.       ptype = TREE_TYPE (base);
  3446.     }
  3447.     }
  3448.   else
  3449.     {
  3450.       error ("type to vector delete is neither pointer or array type");
  3451.       return error_mark_node;
  3452.     }
  3453.   type = TREE_TYPE (ptype);
  3454.  
  3455.   if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
  3456.     {
  3457.       if (extra_warnings && auto_delete == integer_zero_node)
  3458.     warning ("array size expression for delete ignored");
  3459.       rval = build (CALL_EXPR, void_type_node,
  3460.             BID, build_tree_list (NULL_TREE, base), 0);
  3461.       TREE_VOLATILE (rval) = 1;
  3462.       return rval;
  3463.     }
  3464.  
  3465.   size_exp = size_in_bytes (type);
  3466.   iterator = build_decl (VAR_DECL, NULL_TREE, integer_type_node);
  3467.   TREE_REGDECL (iterator) = 1;
  3468.   DECL_INITIAL (iterator) = maxindex;
  3469.   tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
  3470.   TREE_REGDECL (tbase) = 1;
  3471.   DECL_INITIAL (tbase) = build (PLUS_EXPR, ptype, base,
  3472.                 build (MULT_EXPR, integer_type_node, size_exp,
  3473.                        build (PLUS_EXPR, integer_type_node, maxindex, integer_one_node)));
  3474.  
  3475.   block = build_let (input_filename, lineno, iterator, 0, 0, 0);
  3476.   TREE_TYPE (block) = void_type_node;
  3477.  
  3478.   if (auto_delete != integer_zero_node)
  3479.     body = build_tree_list (NULL_TREE,
  3480.                 build (CALL_EXPR, void_type_node,
  3481.                    BID, build_tree_list (NULL_TREE, base), 0));
  3482.   else
  3483.     body = NULL_TREE;
  3484.  
  3485.   if (TYPE_LANG_SPECIFIC (type) && TYPE_USES_VIRTUAL_BASECLASSES (type))
  3486.     body = tree_cons (NULL_TREE,
  3487.               build_vbase_delete (type, build_indirect_ref (tbase, 0)),
  3488.               body);
  3489.  
  3490.   body = tree_cons (NULL_TREE,
  3491.             build_delete (ptype, tbase, auto_delete, LOOKUP_NORMAL, 0),
  3492.             body);
  3493.  
  3494.   body = tree_cons (NULL_TREE,
  3495.             build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
  3496.             body);
  3497.  
  3498.   loop = build_loop (input_filename, lineno,
  3499.              tbase,
  3500.              build (NE_EXPR, integer_type_node,
  3501.                 build (PREDECREMENT_EXPR, integer_type_node,
  3502.                    iterator, integer_one_node),
  3503.                 minus_one),
  3504.              build_compound_expr (body));
  3505.   TREE_TYPE (loop) = void_type_node;
  3506.   TREE_VOLATILE (loop) = 1;
  3507.  
  3508.   if_stmt = build_if (input_filename, lineno,
  3509.               build (GE_EXPR, integer_type_node,
  3510.                  iterator, integer_zero_node),
  3511.               loop, 0);
  3512.   TREE_TYPE (if_stmt) = void_type_node;
  3513.   TREE_VOLATILE (if_stmt) = 1;
  3514.  
  3515.   STMT_BODY (block) = if_stmt;
  3516.   TREE_VOLATILE (block) = 1;
  3517.   return block;
  3518. }
  3519.